实际上我需要在应用图标中显示徽章,但我不知道如何获得徽章以及当应用处于后台或应用未运行时为什么onMessageReceived不会呼叫。
public class Custom_FirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
String activityName;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
if (remoteMessage.getNotification() != null) {
Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.i(TAG, "Data Payload: " + remoteMessage.getData().toString());
try{
//boolean from_bg = Boolean.parseBoolean(remoteMessage.getData().get("from_bg").toString());
Map<String, String> data = remoteMessage.getData();
boolean show_fg = Boolean.parseBoolean(data.get("show_fg"));
.....
答案 0 :(得分:3)
有两种类型的FCM
notification
消息:仅当您的应用位于前台时,使用此消息类型发送有效内容才会触发onMessageReceived()
。
data
消息:仅使用 发送有效内容此特定消息类型会触发onMessageReceived()
,无论您的应用是否位于前台/后台。
参考:https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
答案 1 :(得分:0)
将服务添加到AndroidManifest.xml
。还要确保令牌未过期。
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
如果过期则刷新令牌。
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}