Firebase doesn't show notifications when app not running

10,728

Actually there is an active discussion on this topic, because seems that this behaviour changes based on Android OS customizations performed by manufacturers.

Here one of the answers by a Firebase member on GitHub comment, proves a general solution for Firebase Notifications has not been found yet.

So should not be your fault if it's not working correctly (supposing you have not made mistakes somewhere);

More useful comments in that thread: Comment 1, Comment 2

Share:
10,728
deathangel908
Author by

deathangel908

Full-Stack architect experienced with a variety of applications written in different programming languages, technologies, and platforms. Administrator and maintainer of Linux servers and CI/CD pipelines. Team lead and scrum master with good soft and communication skills. Associate member of the association of body-focused psychologists of Ukraine. Seeking to use my broad experience leading a team and helping to create new architectures.

Updated on June 04, 2022

Comments

  • deathangel908
    deathangel908 about 2 years

    Quick question:

    Am I able to receive notification to default android's Notifications menu (that appears when you swipe status bar down) when my app is not running at all (but installed). If yes - what do I do?

    Detailed description:

    I have implemented Firebase notification into my android app. Here's how:

    Manifest.xml:

    <service
        android:name=".utils.FireBase">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".utils.FireBaseService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@mipmap/ic_launcher" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
    

    FireBaseService.java:

    public class FireBaseService extends FirebaseMessagingService {
        private static final String TAG = FireBaseService.class.getSimpleName();
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.d(TAG, "onMessageReceived: " + remoteMessage.getData().get("message")); 
        }
    }
    

    FireBase.java

    public class FireBase extends FirebaseInstanceIdService {
    
        @Override
        public void onTokenRefresh() {
            // I used this token in rest.
            Log.e("FirebaseToken", FirebaseInstanceId.getInstance().getToken());
        }
    }
    

    Fine, now I want to send notification:

    curl -X POST --data "$DATA" https://fcm.googleapis.com/fcm/send --header "Content-Type:application/json" --header "Authorization:key=$KEY"
    

    $DATA:

    {
      "notification":{ 
          "body": "My notification!"
      },
      "to": "KEY_FROM:FirebaseInstanceId.getInstance().getToken()"
    }
    

    Response:

    {
      "multicast_id": 7830342176959972827,
      "success": 1,
      "failure": 0,
      "canonical_ids": 0,
      "results": [
        {
          "message_id": "0:1485706467606270%8a47512d8a475125"
        }
      ]
    }
    

    Now, when my app is Running I'm able to receive notifications. When my app goes background - notifications goes to Android notifications. Now I close my application (it's neither in foreground nor in background) (By closing I mean - press SQUARE hardware button -> swipe app left, so my app not in task manager anymore). And I want to receive a notification from firebase into Android notifications menu. But nothing happens when I call rest service. I don't care about layout or so I just want the user to be notified.

    I tried notifications with different devices:

    1. Real android device: Xiaomi mi4c based on android 5.1.
    2. Genymotion emulator based on android 7.0 + playmarket installed.
    3. Genymotion emulator based on andoriod 4.4 + playmarket installed.

    Notifications just don't show at all. I observed console, I didn't even find logs about them. I read a lot of stackoverflow posts and as I understand right I should be able to do so.

    How do I archive this goal? If I'm not able to receive notification with firebase - maybe I should use GCM instead?

    Best regards,

  • deathangel908
    deathangel908 over 7 years
    Hm, how curious... I don't see any cases from for using push notifications then. If it's not working while app is NOT running I just can implement same functionality using websockets to my server... According to stackoverflow.com/a/24314088/3872976 this answer GCM supports that feature. I thought Firebase was built on GCM API.
  • MatPag
    MatPag over 7 years
    i'm not sure that the linked solution could work. Because the push notifications blocking system has been added to the custom OS recently enough (the solution you linked is dated 2014). Maybe an option could be a SyncAdapter which check for new push notifications at regular intervals, and in background you can launch something which wake your app and start listening for the new notifications. BTW this is only an idea, i've never tried it
  • Bhavin Patel
    Bhavin Patel about 7 years
    But whatssapp,facebook notification coming even not in recent
  • morha13
    morha13 about 6 years
    Is there a solution yet?
  • deathangel908
    deathangel908 about 6 years
    You can create BackgroundService and start it withing your app or from BroadcastReceiver + BOOT_COMPLETED. Thus your service will be always running and be able to receive FCM