3

If the Android app is killed deliberately by the user or Android, will the FirebaseMessagingService detect a notification when it receives a message?

I have killed my app and then sent a message from Firebase Console. I am unable to receive the notification. Is there a way I can receive a notification even if the app is killed.

public class YumigoBidMessagingService extends FirebaseMessagingService {

private static final String TAG = YumigoBidMessagingService.class.getSimpleName();

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages containing both notification
        // and data payloads are treated as notification messages. The Firebase console always sends notification
        // messages. 
        // [END_EXCLUDE]

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: 
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }
        sendNotification(remoteMessage.getData()+"");
        // 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.
    }
    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, BiddingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("Bid Received")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}
AL.
  • 36,815
  • 10
  • 142
  • 281
androider
  • 982
  • 6
  • 16
  • 32

3 Answers3

2

If the app is killed/force stopped, as mentioned in the answer from the link above:

Force stop is a complete kill of the app -- all processes are killed, all services stopped, all notifications removed, all alarms removed, etc.

Which would result for the app to don't receive any kind of notifications at all, as it was designed for Android since version 3.1, as stated in this answer:

Apps that are in the stopped state do not receive broadcast Intents.

Stopped state is:

when the app is initially installed (before the user runs something in the app) or after a Force Stop. You can find more about this here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols

Just a retrieved a portion from my answer here. Check out the thread, it may also be helpful for you.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • 2
    So is there a way I can achieve this, because I need the notification to be received by the device. I mean whatsapp, facebook and other news feed apps are doing it, so there should be some or the other way – androider Sep 08 '16 at 11:23
  • I'm not entirely sure since I haven't tried it before. However, I think I've read somewhere before where using [START_STICKY](https://developer.android.com/reference/android/app/Service.html#START_STICKY) would help. Do search around the community. Google around. I'm pretty sure this is a common inquiry here. :) – AL. Sep 08 '16 at 11:34
1

There are two types of messages you can send using FCM i.e. notification or data message.

what you're sending is notification message, which if the app is in background is delivered to the system tray instead of your onMessageRecieved() callback.

Try using data messages in which case you will always receive your notification in onMessageRecieved() callback, where you can handle as per you're need instead of depending on system tray.

farhan patel
  • 1,490
  • 2
  • 19
  • 30
  • 4
    I have killed my app. There is no instance of it. onMessageReceived does not work. the app is not in background. It is killed completely – androider Sep 08 '16 at 10:21
  • 1
    As clearly mentioned, even if the app is completely killed, MessagingService will become alive when *Data Message* is sent. – rpattabi Oct 19 '16 at 10:16
  • Simple and working solution! Does work even the app was closed. Maybe it will not work after rebooting the device until the app will be started once - but in that case adding a ´BroadcastReceiver´ with ´ACTION_BOOT_COMPLETED´ may help. – coyer Dec 15 '16 at 16:15
  • 1
    @rpattabi and just where is it clearly mentioned? I am sending data messages and this is not working... please provide a reference for your statement – pjdupreez Jan 10 '17 at 07:08
  • @pjdupreez It is clearly mentioned in the answer: "you will always receive your notification". I have implemented and verified that data messages are received in onMessageReceived() even when the app is not running. Remember it is your responsibility to show notification. You will only get a call to onMessageReceived() unlike notification message where firebase takes care of showing the message. In case of data message, it is upto you to decide what you want to do in your code with the data message you receive. – rpattabi Jan 16 '17 at 07:40
  • 1
    @rpattabi That is not what's happening in my case. When I background the app and send a data message it's received by onMessageReceived() in my FirebaseMessagingService (which interestingly is created and then destroyed for each message). When I kill the app (by swiping from open app list) messages are no longer received in onMessageReceived().I've observed this behaviour on several devices from different manufacturers. – aaronmarino Mar 24 '17 at 11:06
  • I am facing same issue like receiving notification in foreground , background states.. but when app is killed onMessageReceived doesn't gets called and not able to perform any action in killed state. Any solution to that anyone having ? – Jay Rathod Sep 02 '20 at 07:37
0

Shortly speaking, to wake your killed app, you should always send notification with data object to call your notification service class handler FirebaseMessagingService.onMessageReceived() in your application.

Also try sending it not from Firebase console, but to post it from somewhere else (e.g. online testing post service).

Zon
  • 18,610
  • 7
  • 91
  • 99