evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 682 forks source link

Cannot receive notifications from Firebase console on Android while app is in foreground #601

Open psahgal opened 7 years ago

psahgal commented 7 years ago

React Native Version: 0.47.2 Device: LG V20 (Model number LG-H990ds) Android Version: 7.0 App is running in the foreground

I'm testing out using Firebase Cloud Messaging with our React Native app and went through the setup instructions for this library on Android. I saw that the app receives notifications just fine while in the background, but in the foreground, the app doesn't receive any push notifications.

For testing, I set up a new project in the Firebase console and I'm using the Notifications Composer on the Firebase console to send push notifications. All I'm sending is a simple notification with "Hello world!" as the message, and it's not showing up in the notification tray when the app is in the foreground, even though I have the local notification publisher set up. I downloaded the example project and I also wasn't seeing a push notification when the app is in the foreground.

I have two questions:

  1. When the example app is in the foreground, is a notification expected to appear in the system tray when sending it through the Firebase console?
  2. Is there something wrong with my setup? For the example project, I did the following:
    • Cloned the repository
    • Opened the project in Android studio (the folder Examples/simple-fcm-client/android/)
    • Created a new project in the Firebase console with the example project's bundle ID
    • Downloaded the google-services.json file and put it into the project, as the console instructed.
    • Ran npm install in the root folder of the example project (the folder Examples/simple-fcm-client)
    • Ran npm start in the root folder of the example project
    • Hit the "run" button in Android studio and selected my device

I started debugging some of the code in the library and saw some obvious issues. For one, the intent in the onMessageReceived function within the class MessagingService passes in an action named "com.evollu.react.fcm.ReceiveNotification", and there's nothing in the Android manifest listening for an action with that name. After correcting this naming issue and adding an intent filter in the Android manifest, I noticed that the SendNotificationTask assumes the intent has some extras with properties such as body, title, etc., but the intent has a RemoteMessage object as its single extra under the data key.

Please let me know if there's something wrong with my project setup that's causing the issue, otherwise I'm going to have to submit some major changes to the Android side of the code, and update the setup instructions.

evollu commented 7 years ago

What is your payload sending for firebase endpoint? If you send both data and notification in payload, body will be ignored if you are in bakcground.

psahgal commented 7 years ago

I don't think there really is a payload besides the message text. As I mentioned earlier, I'm just using the Notifications composer in the Firebase console. Here's what I have set up in the console:

screen shot 2017-10-09 at 10 41 42 am

I didn't put anything in the advanced options field.

Is this kind of notification intended to work with this library? I noticed that the Android code seems to look for the "custom_notification" field in the payload...do I need to put some JSON into this console under the "Advanced Options" section?

evollu commented 7 years ago

Android notification from FCM is very limited. The notification can only be delivered to notification tray. So you may well end up using custom_notification.

regarding your finding, com.evollu.react.fcm.ReceiveNotification is not designed to be consumed by any Android service. It is just used to be send to Javascript world.

can you add breakpoint on onMessageReceived and see if it is triggered when you send notification in foreground? context.sendOrderedBroadcast(message, null) is where it should trigger JS callbacks

ericacooksey commented 7 years ago

@evollu I am having the exact same issue and I confirmed that onMessageReceived is indeed being triggered. The context is non-null and it calls context.sendOrderedBroadcast. It works fine if the app is not running or if it is backgrounded.

ericacooksey commented 7 years ago

@Elethier I found a workaround:

// This is triggered regardless of app state FCM.on(FCMEvent.Notification, async (notif) => { if (notif.fcm && notif.fcm.body) { // A new notification was received FCM.presentLocalNotification({ title: 'my title', body: notif.fcm.body, big_text: notif.fcm.body, show_in_foreground: true }) } })

evollu commented 7 years ago

well you can do that... why not use custom_notification?

ericacooksey commented 7 years ago

@evollu it's just a proof-of-concept, my point is just that the workaround is to issue a local notification when a push is received.

evollu commented 7 years ago

yes. it is a workaround for android

psahgal commented 7 years ago

@ericacooksey Thanks for the workaround, I think we'll end up using that if we decide to display local notifications. I guess I was a little confused about how much is handled by the library vs. what needs to be done in the project code. Just want to make sure I'm understanding this correctly:

@evollu Is that the expected behavior? Because it seems rather unintuitive that "Hello World" isn't handled, but custom notifications are.

evollu commented 7 years ago

@Elethier you are right.

Niitisha commented 6 years ago

@Elethier - Could you pls post a sample code? I'm new to react native.

xstable commented 6 years ago

I don't get any output, even If I do like @ericacooksey described.

I've checked till RNFIRMessaging.presentLocalNotification(details); and found that the details-Object has data and this Function is fired in /node_modules/react-native-fcm/index.js.

Are there permissions needed to use presentLocalNotification ? I'm on Android 8.0

What woud be the way to send this Payload? FCM.send( {payload-object-here} ) ?