当前位置: 首页 > 知识库问答 >
问题:

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

柯振濂
2023-03-14
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();
        }


    }
}

共有1个答案

耿敏达
2023-03-14

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

确保在所有情况下(前台、后台和应用被杀死)都调用onMessageReceived的唯一方法是从FireBase发送一个仅数据的有效负载。

请查看我对一个类似问题的回答以了解更多细节。

 类似资料: