React-native-fcm: "FCM.presentLocalNotification" is not working in Android

Created on 27 Dec 2016  Â·  9Comments  Â·  Source: evollu/react-native-fcm

I am using "react-native": "^0.35.0" and "react-native-fcm": "^2.5.5"

Notifications are coming, When app running in a background
but when app is in a foreground, not getting any notification.

What I have done:

step 1 :
this.notificationUnsubscribe = FCM.on("notification", notif => {
console.log("notificationUnsubscribe=====>", notif);
/*this console gives me:
{
fcm: {
action: null,
body: "My message",
color: null,
icon: null,
tag: null,
title: "My title"
},
message: "My message"
}
*
/

step 2
/* then called presentLocalNotification */
this.sendRemote(notif); //====> this method defined below
}

sendRemote(notif) {
console.log('when app in foreground send noti', notif);

FCM.presentLocalNotification({
                body: notif.fcm.body
                priority: "high",
                title: notif.fcm.title
                sound: "default",
                show_in_foreground: true,
                tag:"CHAT"
            });

}

I expected that this will give me notification, when app running in a forground but its not working
and also I observed that "presentLocalNotification" not getting triggered.

I also reffered issue no: #140 but still not got the solution

question

Most helpful comment

Following code works for me.

```
FCM.on("notification", notif => {
if(notif.fcm && notif.fcm.body) {
/* Create local notification for showing in a foreground /
FCM.presentLocalNotification({
body: notif.fcm.body,
priority: "high",
title: notif.fcm.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)
status: notif.status
});
}
}

All 9 comments

Could you please post the .on('notification', function(notif) { }) bit?

+1
I'm also having the same trouble. I have already set show_in_foreground to true.
.on('notification') handle was triggered and the notification data is passed on, but no notification banner shows up.

@jvarshaprdxn I observed that "presentLocalNotification" not getting triggered. can you elaborate that?
@GaryLengkong notification doesn't show up could be some notification attribute you pass throw error. try the simplest notification?

Same here on presentLocalNotification, no banner shows when app is on foreground, even though I passed the show_in_foreground: true property.

+1
RN: 0.41, fcm: 6.0.2

my code is something like this:
const notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
if (notif.local_notification) {
return
}
if (notif.opened_from_tray) {
return
}

FCM.presentLocalNotification({
  title: notif.title,
  body: notif.body,
  priority: "high",
  click_action: notif.click_action,
  show_in_foreground: true,
  local: true
})

})


UPDATE:
found my mistake: body was undefined
after i filled the body, its working

Following code works for me.

```
FCM.on("notification", notif => {
if(notif.fcm && notif.fcm.body) {
/* Create local notification for showing in a foreground /
FCM.presentLocalNotification({
body: notif.fcm.body,
priority: "high",
title: notif.fcm.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)
status: notif.status
});
}
}

Nice work @jvarshaprdxn on behalf of the @prdxn team.

thanks @jvarshaprdxn
It also worked for me
FCM.on(FCMEvent.Notification, notif => {
if(notif.fcm && notif.fcm.body) {
console.warn(‘here’)
/* Create local notification for showing in a foreground /
FCM.presentLocalNotification({
body: notif.fcm.body,
priority: “high”,
title: notif.fcm.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)
status: notif.status
});
}
})

[email protected]
[email protected]

Following fixed it for me:

  • Using classpath 'com.google.gms:google-services:3.2.0' on project-level build.gradle
  • Using compile 'com.google.firebase:firebase-core:11.8.0' on app-level build.gradle
  • Adding compile 'com.google.firebase:firebase-messaging:11.8.0' on app-level build.gradle
Was this page helpful?
0 / 5 - 0 ratings