簡體   English   中英

當應用程序處於后台或被殺死時,FCM onMessageReceived() 未被調用

[英]When app is in background or killed FCM onMessageReceived() not called

當應用程序在后台或被殺死時 onRecive onMessageReceived() 沒有被調用,我在通知中只收到 json 。 以下是我的服務 class

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 發送僅數據負載。

請查看我對類似問題的回答以獲取更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM