【问题标题】:Firebase Messaging - Create Heads-Up display when app in backgroundFirebase Messaging - 当应用程序在后台时创建抬头显示
【发布时间】:2017-02-02 13:47:00
【问题描述】:

使用 FCM,当应用处于后台或未运行时,我会在系统托盘中收到推送通知。当应用程序处于前台时,我可以覆盖 onMessageReceived 并使用NotificationCompat 创建我自己的提醒通知。

当我的应用在后台或未运行时,有没有办法创建提醒通知?

谢谢

编辑:这里是我通过 curl 使用的消息有效负载到https://fcm.googleapis.com/fcm/send

{
  "to":"push-token",
    "content_available": true,
    "priority": "high",
    "notification": {
      "title": "Test",
      "body": "Mary sent you a message!",
      "sound": "default"
    },
    "data": {
      "message": "Mary sent you a Message!",
      "notificationKey":"userID/notification_type",
      "priority": "high",
      "sound": "default"
    }
}

【问题讨论】:

    标签: android android-notifications firebase-cloud-messaging heads-up-notifications


    【解决方案1】:

    我找到了解决方案: 我只是从从本地服务器发送到 firebase 服务器的 json 中删除通知标签,然后在 MyFirebaseMessagingService 中生成回调:onMessageReceived() 方法。在此方法中,我使用 NotificationCompat.Builder 类生成本地通知。这是android的代码:

    private void sendNotification(RemoteMessage remoteMessage) {
            Intent intent = new Intent(this, SplashActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra(AppConstant.PUSH_CATEGORY, remoteMessage.getData().get("category"));
            intent.putExtra(AppConstant.PUSH_METADATA, remoteMessage.getData().get("metaData"));
            intent.putExtra(AppConstant.PUSH_ACTIVITY, remoteMessage.getData().get("activity"));
            intent.putExtra(AppConstant.PUSH_ID_KEY, remoteMessage.getData().get("_id"));
    
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(remoteMessage.getData().get("title"))
                    .setContentText(remoteMessage.getData().get("body"))
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0, notificationBuilder.build());
        }
    

    【讨论】:

    • 哇,非常感谢您的提示。如果我们有通知部分,则不会调用 onMessageReceived。它仅在我们删除通知部分并将所有内容移动到数据部分时调用。
    【解决方案2】:

    只有在您的应用处于后台或未运行时使用其他应用时,您才会收到提醒通知。如果您的手机没有被使用,那么您将收到系统托盘通知或锁定屏幕通知。

    如果您使用应用服务器通过 http 协议发送推送通知,那么您甚至可以在发送到 fcm 端点的 json 数据中将优先级设置为高。

    如果您使用的是 firebase 控制台,请在高级通知部分设置下确保优先级较高。

    高优先级将确保您在大多数情况下都能收到提醒通知。

    编辑:这就是您编辑后的 ​​json 应该是什么样子才能成功测试 -

    {
      "to":"push-token",
        "priority": "high",
        "notification": {
          "title": "Test",
          "body": "Mary sent you a message!",
          "sound": "default",
          "icon": "youriconname"
        }
    }
    

    youriconname 是您要设置为通知图标的可绘制资源的名称。

    出于测试目的,我省略了数据。就这么多应该给你提醒通知。

    【讨论】:

    • 感谢您的回复。我将通过 curl 使用的 JSON 有效负载添加到我的问题中。我只是仔细检查了一下,当我在另一个应用程序(或应用程序未运行)时收到声音通知,但我根本没有收到提示通知。
    • 查看您的 json。我会立即向您建议两件事 - 从数据中删除 声音和优先级。仅将其保存在一个位置。此外除非您为 iOS 开发,否则无需设置 content_available。使用此设置进行测试并使用删除数据进行测试后,仅按照建议保留通知,然后查看其行为方式。
    • 另请注意,在使用 fcm 时,您不必使用通知兼容性生成器来发出通知。因为 fcm 自己会处理它。我还要求您暂时删除任何此类通知代码。一旦您从纯 fcm 获得适当的提醒通知,您就可以进一步自定义。
    • 不工作...它没有显示抬头通知。
    • @Shivang 由于在您的上下文中可能不正确的许多其他条件,这可能是可能的。上面解释的过程是绝对正确的。如果它在你的情况下不起作用,那么解释你的上下文原因可能对你不同。
    猜你喜欢
    • 2021-07-27
    • 2020-01-29
    • 2018-01-12
    • 2016-08-08
    • 1970-01-01
    • 2018-11-08
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多