問題:
Here is my manifest這是我的清單
<service android:name=".fcm.PshycoFirebaseMessagingServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.PshycoFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
When the app is in background and notification arrives then the default notification comes and doesn't run my code of onMessageReceived
.當應用程序在後臺並且通知到達時,默認通知會出現並且不會運行我的onMessageReceived
代碼。
Here is my onMessageReceived
code.這是我的onMessageReceived
代碼。 This invokes if my app is running on foreground, not when app in background.如果我的應用程序在前臺運行,而不是在後臺運行時,這將調用。 How to run this code when the app is in background too?當應用程序也在後臺時如何運行此代碼?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
data = remoteMessage.getData();
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String imageUrl = (String) data.get("image");
String action = (String) data.get("action");
Log.i(TAG, "onMessageReceived: title : "+title);
Log.i(TAG, "onMessageReceived: message : "+message);
Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
Log.i(TAG, "onMessageReceived: action : "+action);
if (imageUrl == null) {
sendNotification(title,message,action);
} else {
new BigPictureNotification(this,title,message,imageUrl,action);
}
}
// [END receive_message]
解決方案:
參考一: https://en.stackoom.com/question/2YENG參考二: https://stackoom.com/question/2YENG