When my app gets killed, I can't receive notifications I set from my app server. Previous my gradle was as such:
implementation "com.google.firebase:firebase-messaging:17.0.0"
implementation "com.google.firebase:firebase-invites:16.0.0"
Wih this gradle config I was able to receive notification in foreground, background even when app is swiped
I updated my forebase-messanging so here's my new gradle with firebase-messanging:
implementation "com.google.firebase:firebase-messaging:18.0.0"
Here's my current Service class after update
public class MyService extends FirebaseMessagingService
{
public static final String ACTION = "action";
public static final String EXTRA_DATA = "extra_data";
public static final String MESSAGE = "message";
private RemoteMessage.Notification mNotification;
@Override
public void onNewToken(String refreshedToken) {
super.onNewToken(refreshedToken);
LOGE(TAG, "Refreshed token: " + refreshedToken);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = message.getFrom();
mNotification = message.getNotification();
Map<String, String> data = message.getData();
LOGE(TAG, "Received FCM message from : " + from + " data : " + data);
}
}
With this new version, it it still possible to receive notifications in system-tray when app is swiped ? If so, any ideas ?
Have you defined service class in your manifest like this?:
<service
android:name=".MyService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and if the problem persists even after that. add below method and call it from your onMessageReceived
THis should resolve any kind of problems.
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class); // Put your own launcher Activity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
The above method is taken from Firebase repository .
I have experienced the same on certain phones eg Asus Zenfone. When I swipe the app from the current tasks/apps list, firebase messages are not delivered, all background services/jobs are stopped/never triggered. But on other manufacturers like Samsung, the swiping the app still can receive those messages.
I think there is no other way except of "whitelisting" your app - user has to do it manually, forgot what it's called in Zenfone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.