【发布时间】: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