Firebase消息 - 在后台应用时创建单挑显示

Bri*_*ean 9 android android-notifications heads-up-notifications firebase-cloud-messaging

使用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"
    }
}
Run Code Online (Sandbox Code Playgroud)

Shi*_*ang 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());
    }
Run Code Online (Sandbox Code Playgroud)


Nis*_*bey 5

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

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

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

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

编辑:这就是您编辑的 json 应该如何成功测试 -

{
  "to":"push-token",
    "priority": "high",
    "notification": {
      "title": "Test",
      "body": "Mary sent you a message!",
      "sound": "default",
      "icon": "youriconname"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

为了测试目的,我省略了数据。就这么多应该会给你提示通知。

  • 不工作...它不显示抬头通知。 (5认同)
  • 谢谢回复。我通过curl 添加了我正在使用的JSON 负载到我的问题中。我只是仔细检查了一下,当我在另一个应用程序(或应用程序未运行)中时,我收到了声音通知,但我根本没有收到平视通知。 (3认同)