【问题标题】:Open browser on click of FCM notification when app is killed应用程序被杀死时单击 FCM 通知打开浏览器
【发布时间】:2017-11-02 13:14:39
【问题描述】:

我通过 FCM 推送通知获得了一个我想在浏览器中打开的链接。我是这样做的:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(contentIntent);

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

notificationManager.notify(0, notificationBuilder.build());

但问题是,点击通知时,只有当应用程序处于前台时才会打开浏览器。但是当应用程序被杀死或者它在后台时它不会打开浏览器,它只是打开应用程序。

【问题讨论】:

    标签: android push-notification firebase-cloud-messaging


    【解决方案1】:

    当您的应用在后台并且 FCM 推送通知在请求中包含 notification 有效负载时。然后onMessageReceived 方法不会触发,所以它不会打开任何浏览器。

    如果您想在应用不在前台时打开浏览器,您必须在发送通知请求时使用以下语法(在 Postman 中尝试)。

    {
      "to": "device token",
      "priority" : "normal",
      "data":{
        "title": "title",
        "body": "body"
      }
    
    }
    

    上述请求不应包含notification有效载荷字段,即,

    {
      "to": "device token",
      "priority" : "normal",
      "notification":{   },
      "data":{
      "title": "title",
      "body": "body"
     }
    }
    

    【讨论】:

      【解决方案2】:

      在中使用应用程序上下文 PendingIntent.getActivity

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-05
        • 1970-01-01
        • 2020-05-13
        相关资源
        最近更新 更多