0
votes

I have a React Native app that uses https://github.com/zo0r/react-native-push-notification to manage push notifications. Attempting to migrate from GCM to FCM messages on Android is not working, and I cannot find a way to debug the issue.

I have:

  • Removed all GCM-specific components
  • Followed https://firebase.google.com/docs/cloud-messaging/android/client
  • Followed instructions from react-native-push-notification
  • Successfully built and run the app
  • When sending a notification via the Firebase console - it detects that there is 1 user to send to, but it never gets received on the device
  • When sending via the API https://fcm.googleapis.com/fcm/send, the response indicates success, despite the notification not being received on the device
  • Confirmed device has permission to receive notifications
  • Tested with app in background

AndroidManifest.xml relevant parts:

<manifest...>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application...>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
                android:value="CHANNEL NAME"/>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                    android:value="CHANNEL DESCRIPTION"/>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                    android:resource="@android:color/white"/>
        <activity...>
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>

        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
</manifest>

app/build.gradle includes:

dependencies {
...
    implementation "com.google.firebase:firebase-core:16.0.1"
    implementation "com.google.firebase:firebase-messaging:17.5.0"
}
apply plugin: 'com.google.gms.google-services'

build.gradle includes:

buildscript {
...
    dependencies {
        classpath 'com.google.gms:google-services:4.0.1'
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
...
}

google-services.json has been placed correctly with matching package name.

https://fcm.googleapis.com/fcm/send
POST Request (IDs removed)

Header "Authorization: key=AUTH_KEY"
{
  "to": "DEVICE_TOKEN",
  "notification":
    {
       "title": "Test",
       "body": "Test"
    },
  "priority": 10
}

Response (IDs removed)

{
  "multicast_id": MULTICAST_ID,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results":
  [
    {
      "message_id": "0:MESSAGE_ID"
    }
  ]
}

Firebase console when sending test message to app shows:

This campaign targets 1 app user(s) (100% of potential users)

Indicates device has been registered, and can send the campaign, however it is never displayed on the device.

How can I determine why the message is not displayed on the device?

2
You said the response indicates that the message is received. Doesn't that indicate that your problem isn't with receiving, but DISPLAYING the message? - Mars
Yes, the API response indicates "success", but no notification appears on the device, even if the app is running in the background. - Aeternata

2 Answers

0
votes

In your post request add a data field like :

Header "Authorization: key=AUTH_KEY"
{
"to": "DEVICE_TOKEN",
"notification":
{
   "title": "Test",
   "body": "Test"
},
"data":{
   "body": "Test"
},
"priority": 10
}

And, you need to override onMessageReceived in your RNPushNotificationListenerService.

Doing so, you should be able to handle the notification in onMessageReceived irrespective of the app being in the background or foreground.

See : Handling messages in Firebase

0
votes

The version of react-native-push-notification was not the latest and didn't have the support for Firebase that I thought it had.

Upgrading to the latest version and resolving various dependencies resolved the issue.

Thanks @Aparna for mentioning RNPushNotificationListenerService - it led me down the path to finding the issue.