android - 当应用程序处于后台或被杀死时 FCM onMessageReceived() 未调用

标签 android android-push-notification fcmp

当应用程序在后台运行或被杀死时 onRecive onMessageReceived() 未调用,我在通知中只收到 json。 下面是我的服务类

public class AppFireBaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        // Check if message contains a data payload.
        else if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            sendNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
        }


    }




    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);
        PreferenceHelper preferenceHelper = new PreferenceHelper(this);
        preferenceHelper.putString(Constants.FCM_TOKEN, token);

    }




    public void sendNotification(String title, String messageBody) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Notification notification = mapper.readValue(messageBody, Notification.class);
            Intent intent = null;
            if (notification.getPostType().equalsIgnoreCase("message")) {
                intent = new Intent(this, MessageActivity.class);
                intent.putExtra(Constants.LOGIN_ID, notification.getLoginIdEnc());
            } else {
                intent = new Intent(this, HomeActivity.class);
            }

            Toast.makeText(this, notification.toString(), Toast.LENGTH_SHORT).show();

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);


            String channelId = getString(R.string.default_notification_channel_id);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(title)
                            .setContentText(notification.getMessage())
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            // Since android Oreo notification channel is needed.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "Channel human readable title",
                        NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            }

            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
        }


    }
}

我试过调试它,但也没有打印日志。如果应用程序已打开并正在运行获取正确的通知并且通知已正确构建。当我尝试使用应用程序后台通知消息设置 json 响应时。

最佳答案

如果推送通知在有效负载中有通知部分并且您的应用程序在后台,则永远不会调用 onMessageReceived。通知由 firebase 自动创建和显示。没有办法拦截它并修改通知或为此做任何其他操作。只有在点击通知后,您的应用才会获得控制权。

确保在所有情况下(前台、后台和应用程序终止)调用 onMessageReceived 的唯一方法是从 firebase 发送纯数据负载。

请查看我的answer有关更多详细信息的类似问题。

关于android - 当应用程序处于后台或被杀死时 FCM onMessageReceived() 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364797/

相关文章:

java - Android Firebase 如何从 firebase 获取徽章计数响应

android - 是否可以在任何设备和平台上启动 android 中的 native 文件管理器?

android - 从 android 应用程序的联系人列表中选择一个号码和姓名

android - MediaPlayer seekTo 不起作用

sas - 为什么 PROC FCMP 函数总是返回 33 个字节而不是更多?

sas - sql 不尊重我的 fcmp 函数长度

parameters - 如何定义具有不同数量参数的函数?

android - 如何判断ListView的滚动方向

android - 即使在实现 dats 消息后,FCM 推送通知也会显示原始 json

android - FCM 推送通知未在 Android 自定义操作系统中接收