【问题标题】:how to handle FCM background state in the android如何在android中处理FCM后台状态
【发布时间】:2019-11-26 06:55:19
【问题描述】:

在 android 中,FCM 通知来了,应用程序处于后台状态,当被点击时,它会重定向到默认活动 (LoginActivity),但我想导航到指定的活动 (HomeActivity)。

    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "CHANNEL_1");
    notification/*.setSound(soundUri)*/
            .setSmallIcon(R.drawable.ic_photo_camera_black_24dp)
            .setColor(getResources().getColor(R.color.colorAccent))
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(message);

    Intent intent = new Intent(this, HomeActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
            0);

    notification.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification.build());

【问题讨论】:

  • 请发布一些代码以显示您所做的事情
  • 此代码完美解决了您遇到的其他一些编码错误。
  • 当我发送“notification json”时,它会在应用程序在后台时打开默认活动“通知”:{“body”:“Hello data”,“title”:“这是测试标题数据.", "message": "This is test message.data" } 当我发送 "data json" 时,它会在应用程序在后台时打开特定的活动 "data": { "body": "Hello data", "title ": "这是测试标题数据。", "message": "这是测试消息。数据" }
  • 这段代码写在哪里?

标签: android


【解决方案1】:

您需要从 api/json 部分传递一个活动参数。从您的应用程序端,您将检查活动中殿,然后相应地打开

请参阅此解决方案https://stackoverflow.com/a/39665485/6442746 了解更多信息。

【讨论】:

    【解决方案2】:

    当应用程序处于后台或前台时,您应该发送格式为数据消息以处理 onMessageReceived 中的通知

    在 onMessageReceived() 中收到数据后,您可以显示通知并直接转到您的活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 2019-12-20
      • 2018-12-01
      • 1970-01-01
      • 2018-07-31
      • 2021-11-04
      相关资源
      最近更新 更多