当应用程序处于后台时,不会调用Firebase onMessageReceived(RemoteMessage remoteMessage)

时间:2016-12-07 05:19:06

标签: android firebase firebase-cloud-messaging

在Firebase onMessageReceived(RemoteMessage remoteMessage)中,当您的应用位于Foreground时,系统会调用此方法。因此,点击通知后,您可以打开其他活动,例如NotiicationActivity

但是,如果您的应用程序处于后台,那么该方法将不会被调用,并且在单击通知时,只会打开一个Launcher Activity。

即使我们的应用处于后台,如何在点击通知时打开NotificationActivity

我的代码是这样的:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

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

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        sendNotification(remoteMessage.getNotification().getBody());

    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, NewActivity.class);
        intent.putExtra("key", messageBody);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
        Bitmap icon2 = BitmapFactory.decodeResource(getResources(),
            R.mipmap.ic_launcher);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("FCM Sample")
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setLargeIcon(icon2)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);

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

        notificationManager.notify(new Random().nextInt() /* ID of notification */, notificationBuilder.build());
    }
}

3 个答案:

答案 0 :(得分:4)

onMessageReceived仅在应用程序位于前台时触发。 如果应用程序已落后,您仍然可以收到通知,但不会触发 onMessageReceived

所以我的建议, 在接收活动中,您仍然可以使用以下方式从通知中获取数据:

getIntent().getExtras();

这应该相应地起作用。希望这会有所帮助:)

答案 1 :(得分:0)

尝试按照以下链接:

Firebase Cloud Messaging for Android

答案 2 :(得分:0)

  

在sendNotification()方法中:

 private void showNotification(String message){
    Intent intent=new Intent(this, NotificationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("key", messageBody);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Sample")
            .setContentText(message)
            .setLargeIcon(icon2)
            .setContentIntent(pendingIntent);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());

}