Does not show notification when application is killed in android
See original GitHub issueI 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:
- running
- background running
- killed According to the documentation for FCM there are 3 different types of messages which can be sent via FCM service:
- notification (has predefined fields)
- data (set whatever fields you want)
- 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:
- 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
- OR display a (2) data message when the application is (3) killed?
Thank you!
Issue Analytics
- State:
- Created 5 years ago
- Comments:7
Top GitHub Comments
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.
Don’t pay much attention on this. This - is a class-wrapper made for fcm functions.