1

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.

Mithilesh Izardar
  • 2,117
  • 1
  • 13
  • 24

3 Answers3

1

This happens because on some devices system doesn't allow starting apps or app services by other apps, For FCM notifications Google Play services starts your app service if your app has been killed, when they are killed (ex. Xiaomi phones with MIUI).

Hari Prasad
  • 603
  • 8
  • 18
0

Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.

If you are sending the notification from console, then use the particular key to send the notification from the Advanced Option features.

if (getIntent().getExtras() != null) {
     // Call your NotificationActivity here..
     Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
     startActivity(intent);
    }
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
0

The Android framework advises that apps that have been stopped (not in background) should not be started without explicit user interaction.
FCM follows this recommendation and thus does not deliver messages to stopped apps.
For some phones,Once the app is stopped or killed from recent apps, it was completely stopped by the OS. this can save battery but fcm won't work.

Mike Yang
  • 2,581
  • 3
  • 24
  • 27