React-native-push-notification: On 6.0.0 foreground notification no longer appears on Android

Created on 28 Sep 2020  路  31Comments  路  Source: zo0r/react-native-push-notification

Bug

I have the "notification_foreground" option in AndroidManifest.xml set to "true", as I would like foreground notifications to appear.

In v5.1.1 of this package the notification appears as expected while the app is in the foreground.

When I update to 6.0.0 the notification no longer appears. Notifications continue to appear while the app is in the background ok.

When I roll back to 5.1.1 (with no other changes) the foreground notifications appear again.

Has something changed in 6.0.0 which would affect this behaviour?

Thanks

Environment info

react-native info output:

 // paste it here

Library version: 6.0.0

Steps To Reproduce

  1. Install 6.0.0 of this library
  2. Set "notification_foreground" to true in AndroidManifest.xml
  3. Run the app on Android
  4. Trigger a remote notification while the app is open in the foreground
  5. Don't see the notification appear
  6. Roll back to 5.1.1 and repeat the steps
  7. See the notification appear while the app is open in the foreground
    ...

Describe what you expected to happen:

Foreground notification behaviour doesn't change between version 5.1.1 and 6.0.0.

Reproducible sample code

Most helpful comment

Hey I'm using version 7.0.0 and I'm experiencing same problem. I've created my channel:

PushNotification.createChannel(
      {
        channelId: INTERNAL_NOTIFICATIONS_CHANNEL, // (required)
        channelName: '..., // (required)
        channelDescription: '...', // (optional) default: undefined.
        soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
        importance: 4, // (optional) default: 4. Int value of the Android notification importance
        vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
      },
      (created) => console.log({created}), // (optional) callback returns whether the channel was created, false means it already existed.
    );

And I'm reacting to notification like this:

  PushNotification.localNotification({
          id: messageId,
          title: notification.title, // (optional)
          message: notification.body, // (required)
          userInfo: data,
          channelId: INTERNAL_NOTIFICATIONS_CHANNEL,
        });

It works when application is in background but not when it's in foreground. Do you know what could I be doing wrong?

All 31 comments

Hi @gavrichards

Unable to reproduce with the example project.

I suggest you to take a look at your settings such as channel with the version 6.0.0.

Regards,

Is it necessary to use channels? I don't think I have any use for them.

It's also true with local notifications

I'm finding that remote notifications _do_ work though, just not when the app is in the foreground. Is this expected?

I read "To use channels, create them at startup" to mean if you _want_ to use channels... but I didn't believe I had any need for them.

I am currently migrating from React Native Notifications where this concept doesn't exist.

I do not have any need for local notifications.

Thanks for your speedy help.

With the app open in the foreground, I don't see the notification, but I do see this in my console:

NotificationHandler: {"channelId": null, "color": null, "data": {}, "finish": [Function finish], "foreground": true, "id": "541100095", "message": "test", "priority": "high", "sound": null, "tag": null, "title": null, "userInteraction": false, "visibility": "private"}

(Optional) From Android 8.0 (API level 26) and higher, notification channels are supported and recommended. FCM provides a default notification channel with basic settings.

-- https://firebase.google.com/docs/cloud-messaging/android/client

This suggests it is not necessary to use a channel, and a default one is provided.

With the app open in the foreground, I don't see the notification, but I do see this in my console:

NotificationHandler: {"channelId": null, "color": null, "data": {}, "finish": [Function finish], "foreground": true, "id": "541100095", "message": "test", "priority": "high", "sound": null, "tag": null, "title": null, "userInteraction": false, "visibility": "private"}

And what do you see in the previous version ?

But yes, channel are not necessary since Firebase create one for basic notification.

IMO the reason why you don't get the notification in foreground, is because Firebase doesn't provide the information about the channel used:
NotificationHandler: {"channelId": null, ...}

In the previous version, channels were created on the fly, which avoid this issue and you should have more than one channel in 5.1.1.

In 5.1.1 I see the notification at the top of the screen, and this in the console:

NotificationHandler: {"color": null, "data": {}, "finish": [Function finish], "foreground": true, "id": "-1404223771", "message": "test", "sound": null, "title": null, "userInteraction": false}

You can test changes on dev branch.

To install this version:

npm install zo0r/react-native-push-notification#dev

OR

yarn add zo0r/react-native-push-notification#dev

Make sure your build cache is cleaned.

I just change this by adding the default channelId of firebase from:

https://github.com/firebase/firebase-android-sdk/blob/fe51be95316ad7e72ba0ecd98776415ff9109b4e/firebase-messaging/src/main/java/com/google/firebase/messaging/CommonNotificationBuilder.java#L64

Even if I still recommend to use your own channel, it's not wanted to get the described behaviour.

Thanks - I have tested this. The notification still doesn't appear, but it looks like this in the console:

NotificationHandler: {"channelId": "fcm_fallback_notification_channel", "color": null, "data": {}, "finish": [Function finish], "foreground": true, "id": "-889426055", "message": "test", "priority": "high", "sound": null, "tag": null, "title": null, "userInteraction": false, "visibility": "private"}

And what's are your channels on the phone ? (from settings)

I'm afraid I don't know - where would I find this? I'm usually an iPhone user.

In settings, you should find a "notifications" menu and a list of applications. Find your application and by open the item, you will find a list of channels if any.

Or you can use:

PushNotification.getChannels(function (channel_ids) {
  console.log(channel_ids); // ['channel_id_1']
})

{"channels": ["fcm_fallback_notification_channel", "com.guichaguri.trackplayer"]}

The second one appears to be related to an audio library I'm using.

Did you clean/rebuild your app when you installed the dev branch ?

I have fully cleaned it now, uninstalled the app and reinstalled.

Channels is now appearing as:

{"channels": ["com.guichaguri.trackplayer"]}

And the notification appears as:

NotificationHandler: {"channelId": "fcm_fallback_notification_channel", "color": null, "data": {}, "finish": [Function finish], "foreground": true, "id": "-475178898", "message": "test", "priority": "high", "sound": null, "tag": null, "title": null, "userInteraction": false, "visibility": "private"}

But I do not see a notification while the app is in the foreground. What should I be expecting?

I will release a new version soon, but you will have to create a channel to achieve this.
Firebase doesn't create the channel if the notification is in foreground. On background it's ok (then after in foreground too with the change).

But it's no more the responsibility of the library to create the channel. In 5.1.1, I'm sure a channel is created by the library when this case occurs. This behavior has been deprecated.

That's unfortunate, it was great when this just worked out of the box, when other libraries don't support showing remote notifications in foreground at all. What would you suggest as a temporary workaround until a new release is published? Would I be best sticking with 5.1.1?

Even with the release, you will need to define a channel.
You can define the channel id to use inside the notification payload, this is a kind of workaround.

I think I understand this better now having read more of the Google docs, and appreciate the direction you are taking.

One thing I hope you could clarify for me:
We manage many apps using the same codebase for many customers. They all use the same back-end systems, including for sending push notifications. If we have to add a channelId to the payload, this will be sent in notifications to all of the existing apps, many of which won't be updated for a long time to the new codebase. Do you know how existing apps will handle this? Will the notifications suddenly stop working for those apps, or if an existing app has no channels, will it still show the notifications sent with a specific channelId?

Thanks

I just released the version 6.1.0.
In your case you can create a channel and define it as default channel for FCM notifications in AndroidManifest, the version 6.1.0 allow this also for foreground notifications.
Then you don't need to specify the channel id in your backend. This should keep the compatibility with old versions.

Thank you, I will give this a try tomorrow.

This works perfectly, thank you so much for your help.

Hey I'm using version 7.0.0 and I'm experiencing same problem. I've created my channel:

PushNotification.createChannel(
      {
        channelId: INTERNAL_NOTIFICATIONS_CHANNEL, // (required)
        channelName: '..., // (required)
        channelDescription: '...', // (optional) default: undefined.
        soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
        importance: 4, // (optional) default: 4. Int value of the Android notification importance
        vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
      },
      (created) => console.log({created}), // (optional) callback returns whether the channel was created, false means it already existed.
    );

And I'm reacting to notification like this:

  PushNotification.localNotification({
          id: messageId,
          title: notification.title, // (optional)
          message: notification.body, // (required)
          userInfo: data,
          channelId: INTERNAL_NOTIFICATIONS_CHANNEL,
        });

It works when application is in background but not when it's in foreground. Do you know what could I be doing wrong?

+1

+1

For all the "+1".
First of all, this is not message which help to debug this case.
Secondly, you probably copy and paste the AndroidManifest which include a line to hide notification in foreground.
Regards,

Was this page helpful?
0 / 5 - 0 ratings