>实际上我需要在应用程序图标中显示徽章,但我不知道如何获取徽章以及为什么当应用程序在后台或应用程序未运行时,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"));
.....
FCM 有两种类型
notification
消息:仅当应用处于前台时,发送具有此消息类型的有效负载才会触发onMessageReceived()
。
data
消息:发送仅包含此特定消息类型的有效负载会触发onMessageReceived()
无论您的应用是在前台还是后台。
参考: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
将服务添加到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);
}