【问题标题】:Firebase Cloud Messaging onMessageReceived not working with ShortcutBadgerFirebase 云消息传递 onMessageReceived 无法与 ShortcutBadger 一起使用
【发布时间】:2018-12-15 20:48:09
【问题描述】:

我正在使用 Firebase Cloud Messaging 发送通知,并使用 ShortcutBadger 来更新我们应用徽章上的未读消息计数。我扩展了FirebaseMessagingService 类并实现了覆盖方法onMessageReceived,这似乎工作正常,因为当我的应用程序在后台或被杀死时我会收到通知。但是,当我在此方法中添加 ShortcutBadger 调用时,我不再收到通知,并且 ShortcutBadger 调用不起作用。

ShortcutBadger 需要一个Context,并且由于 FirebaseMessagingService 扩展了 Service,它是一个上下文,这就是为什么我只是传递 this。我的代码的其他部分也有 ShortcutBadger,所以我知道它可以工作。

任何建议将不胜感激。

public class Notifications extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        ShortcutBadger.applyCount(getApplicationContext(), 1); // <-- now is working
    }

}

编辑:服务器端代码

这是发送 Firebase 消息的服务器端代码。

Notification notification = new Notification.Builder(null)
        .title(msg.getTitle())
        .badge(msg.getBadge())
        .body(msg.getMessage())
        .build();


Message message = new Message.Builder()
        .timeToLive(timeToLive)
        .delayWhileIdle(true)
        .notification(notification)
        .addData("text", msg.getMessage())
        .addData("title", msg.getTitle())
        .addData("line1", msg.getSubtitle())
        .addData("badge", String.valueOf(msg.getBadge()))
        .addData("qummute_notification", "true")
        .build();

Result result = send(message, deviceKey, retries);

有人建议我在没有通知负载的情况下发送消息。会尝试一下,看看会发生什么。

【问题讨论】:

  • 您是否尝试过使用应用程序上下文?
  • 不幸的是,我尝试了“this”和“this.getApplicationContext()”,但都没有成功。感谢您的建议。
  • 您从服务器发送什么类型的 Firebase 消息?这很重要

标签: android android-service firebase-cloud-messaging


【解决方案1】:
  1. 你确定你在测试时没有打电话给ShortcutBadger.removeCount(Context context)吗?
  2. 尝试使用ShortcutBadger.applyCountOrThrow(Context context, int badgeCount) 看看是否抛出任何异常。
  3. onMessageReceived(RemoteMessage remoteMessage) 在后台线程上调用。您正在测试的手机/启动器可能正在使用无法从后台线程创建和显示的徽章。

【讨论】:

    【解决方案2】:

    您需要从服务器端处理这种情况。从您的 JSON 中删除 Notification 并使用 data。 移除通知孔对象, “通知”: { "title": "我的标题,如果有的话", “身体”:“位置”, “声音”:“默认” },

    从您发送需要更改该代码的通知的位置, 对于服务器端代码应该如下所示,即使应用程序在后台或被终止,您的 onMessageReceived 方法也会调用。

    {
      "registration_ids": [
        "cBbGxlj5JRI:APA91bFRLp3tDoBo_P5lGYGUZ4F6iJ55yBJq4GInL7RVV2asHR5PovqnMX-ekIdl_xOYyRyJmSe3SkxYMa3mzIvqml3MmLYZUh8JWygixIebhzhb_l4xv0Q-v3werKl_UO069G1uOyvq"
      ],
      "data": {
        "title": "alt_ctgry - Vehicle Number - extra detail(if any)",
        "body": "location"
      },
      "priority": "high"
    }
    

    如果您想创建通知,请编写您自己的通知代码。 对于通知数据访问使用下面的代码,

    @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            Log.e("FCM", "Call onMessageReceived\n" + remoteMessage + "");
    
            String title = remoteMessage.getData().get("title");
            String body = remoteMessage.getData().get("body");
    
            createNotification(title, body);
        }
    

    【讨论】:

      【解决方案3】:

      这似乎工作正常,因为当我的应用程序在后台或被杀死时我会收到通知

      不,onMessageReceived 在您的应用处于前台而不是后台时调用,如果您有通知负载。

      在您的服务器端代码中添加通知负载,因此以前您的onMessageReceived 在后台应用程序时也不会被调用,但操作系统会从您的负载中显示通知。

      如果您想一直处理推送,则需要删除通知负载,但请记住,如果您有 iOS 应用,它可能会破坏 iOS 上的推送。

      【讨论】:

      • 谢谢。我们今天将删除通知负载并测试它是否有效。
      猜你喜欢
      • 2018-08-04
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多