Firebase Messaging - 在应用程序后台时创建悬浮通知窗口

10

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

有没有办法在我的应用程序在后台或未运行时创建一个 heads-up 通知?

谢谢

编辑:供参考,这是我通过 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"
    }
}
2个回答

6

我找到了解决方案: 我只是从我们的本地服务器发送到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不会被调用。只有当我们删除通知部分并将所有内容移动到数据部分时,它才会被调用。 - nuynait

5
只有在您的应用程序在后台运行或未运行时,您才会收到提醒通知。如果您的手机没有被使用,则将收到系统托盘通知或锁屏通知。
如果您使用应用程序服务器通过http协议发送推送通知,则可以在发送到fcm终点的json数据中设置优先级为高。
如果您使用Firebase控制台,则请确保在高级通知部分设置中将优先级设置为高。
高优先级将确保您在大多数情况下收到提醒通知。
编辑:以下是成功测试的编辑json示例 -
{
  "to":"push-token",
    "priority": "high",
    "notification": {
      "title": "Test",
      "body": "Mary sent you a message!",
      "sound": "default",
      "icon": "youriconname"
    }
}

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

出于测试目的,我省略了数据。只有这么多就足以提醒您。


3
谢谢回复。我已经在我的问题中添加了我使用curl的JSON负载。我刚刚仔细检查了一下,当我在另一个应用程序(或未运行的应用程序)中时,我会收到声音通知,但我根本没有收到悬浮通知。 - Brien Crean
看了你的JSON,我会立即建议你两件事 - 从数据中删除声音和优先级。只保留一个位置。另外,除非你是为iOS开发,否则没有必要设置content_available。一旦使用这个设置进行测试,并且按照建议仅保留通知进行测试,然后查看它的行为如何。 - Nishant Dubey
请注意,使用 FCM 时,您不必使用通知兼容构建器来创建通知。因为 FCM 本身会处理它。我还要求您暂时删除任何此类通知代码。一旦您从纯 FCM 中获得适当的 heads up 通知,那么您可以进一步自定义。 - Nishant Dubey
5
无法工作......它没有显示悬浮通知。 - Shivang
@Shivang,这可能是由于您的情况下存在许多其他条件,这些条件可能不适用于您的情况。上面解释的过程肯定是正确的。如果在您的情况下无法正常工作,请解释您的上下文,原因可能对您而言是不同的。 - Nishant Dubey
@Nishant:这是我们正在使用的JSON:请检查帖子。 - Shivang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接