2

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,

Community
  • 1
  • 1
deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • You could read this article it might be helpful for some else having same problem too [link] (https://medium.com/@shayan.ta69/how-to-handle-fcm-notification-in-backgrounded-android-applications-29229c4f9975) – Karan sharma Apr 03 '18 at 13:09

1 Answers1

3

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

MatPag
  • 41,742
  • 14
  • 105
  • 114
  • 1
    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 http://stackoverflow.com/a/24314088/3872976 this answer GCM supports that feature. I thought Firebase was built on GCM API. – deathangel908 Jan 29 '17 at 19:15
  • 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 – MatPag Jan 29 '17 at 20:27
  • But whatssapp,facebook notification coming even not in recent – Bhavin Patel Apr 29 '17 at 06:55
  • Is there a solution yet? – morha13 May 23 '18 at 21:58
  • 1
    You can create [BackgroundService](https://developer.android.com/training/run-background-service/create-service) and start it withing your app or from [BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver) + `BOOT_COMPLETED`. Thus your service will be always running and be able to receive FCM – deathangel908 Jun 09 '18 at 16:50