当应用程序处于后台或已终止时,FCM onMessageReceived()不被调用

时间:2019-10-13 15:02:45

标签: 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响应时。

1 个答案:

答案 0 :(得分:1)

如果推送通知的有效负载中包含通知部分,而您的应用程序处于后台,则永远不会调用onMessageReceived。通知是由Firebase自动创建和显示的。没有办法拦截此事件并修改通知或对此做任何其他操作。单击通知后,您的应用程序才能获得控制。

确保onMessageReceived在所有情况下(前景,背景和应用程序被杀死)都被调用的唯一方法是从Firebase发送仅数据有效负载。

请检查我的answer类似问题以获取更多详细信息。