question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does not show notification when application is killed in android

See original GitHub issue

I use Firebase Cloud Messaging (FCM) HTTP Legacy API protocol to send push notifications in JSON to android mobile devices. For the client side I use react-native-fcm library. The aim is to send the notification to the particular devices when the application is in 3 states:

  1. running
  2. background running
  3. killed According to the documentation for FCM there are 3 different types of messages which can be sent via FCM service:
  1. notification (has predefined fields)
  2. data (set whatever fields you want)
  3. mixed (notification + data). The logic of listening the event for incoming message on the client side using react-native-fcm is the next:

this.notificationEmitterSubscription = FCM.on(FCMEvent.Notification, notif => { if(notif && notif.fcm){ //received from Firebase if(!notif.local_notification && notif.title){ let badge = parseInt(notif.badge); FCM.setBadgeNumber(badge); this.showNotification(notif.title, notif.body, badge); } //notification is clicked if(notif.opened_from_tray){ FCM.setBadgeNumber(0); this.executeNavigateAction(notif.fcm.action); //this method just navigates user to a particular screen in the application } } });

Show notification method is implemented in this way:

showNotification(title, body, badge) { FCM.presentLocalNotification({ body: body, priority: "high", title: title, sound: "default", large_icon: "ic_launcher",// Android only icon: "ic_launcher", show_in_foreground :true, /* notification when app is in foreground (local & remote)*/ vibrate: 300, /* Android only default: 300, no vibration if you pass null*/ lights: true, // Android only, LED blinking (default false) badge: badge, local: true, click_action: NAV_SCREEN_NAME }); }

notif.title, notif.body and notif.badge are the fields which are set in data section of the message when sending it via FCM API. In other word the message is sent in the (3) mixed form: { "registration_ids" : ["FCM_device_token_1", "FCM_device_token_2"], "notification" : { "title" : "fcm notification message title", "body" : "fcm notification message body", "badge" : 111 }, "data" : { "title" : "fcm data message title", "body" : "fcm data message body", "badge" : 222 } }

If the message is sent as (1) notification (without “data” section in the message, in this case some changes in the reading the fields are necessary, to change notif.title -> notif.fcm.title, but this is not the main point in the question) or mixed (3) then the listener for the notification is NOT triggered when application is (2) background running and (3) killed. As a result, the badge number is not set. BUT despite the fact that the method showNotification(title, body, badge) is not called (because the event listener is not triggered) the message IS shown. It seems that react-native-fcm has internal implementation for this situation to show (1) notification and (3) mixed messages automatically when application is not running. In other words, the listener IS called for (1) notification and (3) mixed messages only when the application is (1) running and IS NOT called when the application is in the (2) background or (3) killed and does NOT show the badge number. However, the message itself IS shown for all situations.

Another approach is to send a (2) data message. This type of FCM message triggers the listener (notificationEmitterSubscription) for all states of the application: (1) running and (2) background running and (3) killed. As a result, badge number is set in all these states. However, despite the fact that method showNotification(title, body, badge) is called whenever a data FCM message is received, method FCM.presentLocalNotification does NOT display the message if the application is killed.

Thus, in few words, I have a question. How to:

  1. EITHER display a badge number when (1) notification or (3) mixed message is received and the application is in (2) background running or (3) killed
  2. OR display a (2) data message when the application is (3) killed?

Thank you!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
iluhakhurtincommented, May 25, 2018

OK, speaking about iOS it works in a different way. Firstly, to be shown on the device the format of the message to be sent via fcm api is different - it must be in predefined fields (notification) format. Cannot give your an example now, because don’t have access to the pc with the code. Moreover, some changes for your firebase account (adding APN certificate to it) are necessary to send the messages to iOS. Also, some changes for xCode are also necessary. So, in any case, you cannot just build for iOS and run.

1reaction
iluhakhurtincommented, May 25, 2018

Don’t pay much attention on this. This - is a class-wrapper made for fcm functions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notification not showing in Android 8 and above when app is ...
i want notification when app is killed using services ...
Read more >
Android device cannot receive push notifications when an ...
Android device cannot receive push notifications when an application is force stopped ("killed") · Go to Settings > Battery, and disable " ...
Read more >
How to get push notification from an app in Android even if ...
Go to settings –> notification panel & status bar –> notification center, then find the app, then activate “allow notifications” and also “priority...
Read more >
[Android] Notification not showing when app is killed #1888
Issue I am trying to display a notification, when a data-only notification is received from a third party platform. Its all good on...
Read more >
Notifications Not Shown - Mobile Push
Step 2: Settings --> Apps or Notification Manager --> Advanced or Notify for your app --> Ignore Battery Optimizations, select the app and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found