【问题标题】:Firebase onMessageReceived(RemoteMessage remoteMessage), is not called when the app is in background [duplicate]Firebase onMessageReceived(RemoteMessage remoteMessage),当应用程序在后台时不被调用[重复]
【发布时间】:2017-04-21 22:51:21
【问题描述】:

在 Firebase onMessageReceived(RemoteMessage remoteMessage) 中,当您的应用处于前台时,将调用此方法。因此,点击通知后,您可以打开不同的 Activity,例如 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());
    }
}

【问题讨论】:

标签: android firebase firebase-cloud-messaging


【解决方案1】:

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

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

getIntent().getExtras();

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

【讨论】:

  • 谢谢这个作品,我刚做了这个。 if (getIntent().getExtras() != null) { // I Wrote here. Intent intent = new Intent(MainActivity.this, NotificationActivity.class); startActivity(intent); }
  • 不用担心@Shekhar,请投票,以便其他人知道此解决方案有效。谢谢。
  • 消息有数据消息和通知消息两种。无论应用程序是在前台还是后台,数据消息都在 onMessageReceived 中处理。仅当应用程序处于后台时通知消息
【解决方案2】:

尝试点击以下链接:

Firebase Cloud Messaging for Android

【讨论】:

  • 本教程仅在您的应用程序处于前台时有效。即使应用程序在后台,甚至没有启动,我也想打开 NotificationActivity。
【解决方案3】:

在你的 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());

}

【讨论】:

猜你喜欢
  • 2021-01-01
  • 1970-01-01
  • 2017-04-22
  • 2017-01-14
  • 2020-02-10
相关资源
最近更新 更多