问题
I am using Firebase to send push notifications to my app. My app supports till API level 27.I am using FirebaseMessagingService to receive notifications. Inside onMessageReceived() method I am taking data payload and creating own notification in tray. From console I am sending only data payload since i dont want app to create system notifications if the app is in background. Now I am able to get callback in onMessageReceived() when app is in foreground and background. But the issue is its not getting invoked when the app is cleared from the memory(or killed). How can I get callback in onMessageReceived when my app is killed?
Manifest.xml
<service android:name=".service.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".service.FcmMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="promotions" />
FcmMessagingService.java
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Logger.v(Logger.TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Logger.v(Logger.TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
//JSONObject json = new JSONObject(remoteMessage.getData().toString());
NotificationHandler.handleNotification(this, formNotificationObject(null));
} catch (Exception e) {
Logger.e(Logger.TAG, "Exception: " + e.getMessage());
}
}
}
private Notification formNotificationObject(JSONObject data) {
//creating notifcation object here
}
build.gradle
compile "com.google.firebase:firebase-messaging:11.4.2"
compile 'com.facebook.android:facebook-android-sdk:4.29.0'
compile "com.android.support:appcompat-v7:26.0.0"
Payload
{
"to": "cogzTKfWsXI:APA9................plSjNu",
"mutable_content": false,
"time_to_live": 20,
"data": {
"messageid": 10,
"title": "Test",
"body": "Testing",
"image": "",
"expiry": "2018-11-13 11:35:11"
}
}
回答1:
An Android app is made up of different Activities and Services and Android differentiates between an application's stopped state and it's activity's stopped state. An activity is stopped when a user swipes an app out of memory from the recent app list. Applications, on the other hand, are in a stopped state when they are:
- first installed but are not yet launched and
- when they are manually stopped by the user in Manage Applications source
The problem
Because some Android phone manufacturers mis-interpret this spec (see this and this) by putting apps into a stopped state when apps are swiped from the recents view, it seems that there is no way to reliably get a push notification delivered to all Android devices unless you are WhatsApp, Gmail, Twitter etc. and have been whitelisted by these manufacturers.
The fix
The net result is, there are many device-specific and os version-specific factors that play in to how deliverable our app push notifications are. Things to try:
On Android X to 7.1, enable autostart: Settings > Security > Permissions > Autostart
On Android 8.0 the Auto-start option was removed, so now you can go to Settings > Battery > Battery Optimisation > (Three dots menu on left-top corner) Advanced optimisation > Turn off Advanced optimisation
回答2:
If you are sending the Message in "Firebase Console - Notifications composer", it always used in foreground.
There are two type of message.
- Notification Message
- Data Message
In case of Notification Message, it is handled just in Foreground. But in case of Data Message, it can be handled in Background.
And sending the message through "Firebase console - Notification", means that the message type is "Notification Message".
If you want to send the type of Data Message, you should use Firebase Cloud Functions or others.
You can check following link: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
And this link: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
来源:https://stackoverflow.com/questions/48537156/onmessagereceived-not-getting-called-when-app-is-killed