I am using FCM cloud messaging and it is working fine when the app is in the background but not killed or when the app is running. Once the app is stopped or killed from recent apps it doesn't receive any notification.
Even it doesn't receive the old messages once the app is started.
code for onMessageReceived()
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("Nick");
String myCustomKey1 = data.get("Room");
testNotification(myCustomKey,myCustomKey1);
}
private void testNotification(String message,String title) {
Intent intent = new Intent(this, ChatActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.sham_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
server side code for sending notification
{
"to" : "token-key",
"data" : {
"title":"title",
"body":"body",
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
manifest code for firebase services and notification
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/narendramodi_icon" />
android:resource="@drawable/sham_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/accent" />
<service android:name=".firebase.MyFirebaseMessagingService"
android:exported="false">
<intent-filter android:priority="10000">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Any help is appreciated.