问题
Actually i need to show badge in app icon but i don't know how to get badge and why onMessageReceived not call when app is in background or app is not running.
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"));
.....
回答1:
There are two types of FCM
notification
Messages: Sending a payload with this message type triggers onMessageReceived()
only when your app is in foreground.
data
Messages: Sending a payload with only this specific message type triggers onMessageReceived()
regardless if your app is in foreground/background.
Reference: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
回答2:
Add service to AndroidManifest.xml
. Also make sure token is not expired.
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Refresh Token if it is expired.
@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);
}
来源:https://stackoverflow.com/questions/44152037/when-an-app-is-in-background-onmessagereceived-method-is-not-firing