wix / react-native-notifications

React Native Notifications
MIT License
3.25k stars 765 forks source link

Notification not displaying in Notification Center for Android but can see it on console. #869

Closed dev4bitcoin closed 2 years ago

dev4bitcoin commented 2 years ago

The notification is not displaying in the Notification Center but I can see it on console whenever notification is arrived. It only happens on Android though. iOS is working fine. I went through all the open and closed bugs but none of the solutions are working.

Below is the version I am using.

"react-native": "0.64.3", "react-native-notifications": "^4.3.0",

Also, notification logged twice when it I arrived.

vitorl-s commented 2 years ago

I'm also facing the same problem, did you solved it?

dev4bitcoin commented 2 years ago

I just removed the following from registerNotificationReceivedForeground and registerNotificationReceivedBackground.

Remove this line. Notifications.postLocalNotification(notification.payload);

CRathod1220 commented 2 years ago

Hey is anyone got solutions ?. I am also facing this issue, notifications arrived but it not visible in foreground state.while app is in background state then notification is visible there.

CRathod1220 commented 2 years ago

Degrading react-native-notification version from 4.3.1 to 4.2.1 worked for me !

I am using below version : 

"react-native": "0.68.2",
"react-native-notifications": "4.1.2",
CRathod1220 commented 2 years ago

The notification is not displaying in the Notification Center but I can see it on console whenever notification is arrived. It only happens on Android though. iOS is working fine. I went through all the open and closed bugs but none of the solutions are working.

Below is the version I am using.

"react-native": "0.64.3", "react-native-notifications": "^4.3.0",

Also, notification logged twice when it I arrived.

Try "react-native-notifications": "4.1.2" will worked !

dwardin commented 2 years ago

Hey gents, hopefully useful for anyone else who comes across this. You need to register for a channel when you're on Android, and you need to decorate the local notification payload with a channel id, and then it works great.

Not so much a library-specific thing rather than an Android-specific setup for notification - should be better documented but the library works fine. (I'm on 4.3.1)

Channel creation:

  Notifications.setNotificationChannel({
        channelId: 'my-notification-channel-id',
        name: 'My App',
        importance: 5,
        description: 'My Notification',
        enableLights: true,
        enableVibration: true,
        showBadge: true,
        vibrationPattern: [200, 1000, 500, 1000, 500],
  });

Code I used to add the channel id to incoming remote notifications, and to de-dupe the handling of them (local notification will trigger another received foreground notification.

  Notifications.registerNotificationReceivedForeground((notification: Notification, completion) => {
      if (callback)
        callback(notification);

      if (Platform.OS == 'android') {
        console.log(JSON.stringify(notification.payload));

        // check if we have a payload, and if a channel was supplied - if no channel then we have a remote push notification
        // if channel was supplied, it's a callback to the local notification we're creating inside of this if statement
        if (!notification.payload || (!notification.payload.android_channel_id && !notification.payload["gcm.notification.android_channel_id"])) {
          notification.payload.android_channel_id = "my-notification-channel-id";

          let nId = Notifications.postLocalNotification(notification.payload);
          console.log(nId);
        }
      }

      // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
      completion({alert: true, sound: true, badge: false});
    },
  );

The code in the library uses this to lookup the channelId from the payload:

  public String getChannelId() {
        return getBundleStringFirstNotNull("gcm.notification.android_channel_id", "android_channel_id");
  }

If nothing is found, it will use the default channel properties baked into the library.

  final private String DEFAULT_CHANNEL_ID = "channel_01";
  final private String DEFAULT_CHANNEL_NAME = "Channel Name";

So in theory, you could just register a channel with this ID and channel name, and it would just work without the other code to decorate the payload. Naturally, that's probably only appropriate for local testing/development.

Hope this is useful!

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

GeoFro commented 2 years ago

Hey gents, hopefully useful for anyone else who comes across this. You need to register for a channel when you're on Android, and you need to decorate the local notification payload with a channel id, and then it works great.

Not so much a library-specific thing rather than an Android-specific setup for notification - should be better documented but the library works fine. (I'm on 4.3.1)

Channel creation:

  Notifications.setNotificationChannel({
        channelId: 'my-notification-channel-id',
        name: 'My App',
        importance: 5,
        description: 'My Notification',
        enableLights: true,
        enableVibration: true,
        showBadge: true,
        vibrationPattern: [200, 1000, 500, 1000, 500],
  });

Code I used to add the channel id to incoming remote notifications, and to de-dupe the handling of them (local notification will trigger another received foreground notification.

  Notifications.registerNotificationReceivedForeground((notification: Notification, completion) => {
      if (callback)
        callback(notification);

      if (Platform.OS == 'android') {
        console.log(JSON.stringify(notification.payload));

        // check if we have a payload, and if a channel was supplied - if no channel then we have a remote push notification
        // if channel was supplied, it's a callback to the local notification we're creating inside of this if statement
        if (!notification.payload || (!notification.payload.android_channel_id && !notification.payload["gcm.notification.android_channel_id"])) {
          notification.payload.android_channel_id = "my-notification-channel-id";

          let nId = Notifications.postLocalNotification(notification.payload);
          console.log(nId);
        }
      }

      // Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
      completion({alert: true, sound: true, badge: false});
    },
  );

The code in the library uses this to lookup the channelId from the payload:

  public String getChannelId() {
        return getBundleStringFirstNotNull("gcm.notification.android_channel_id", "android_channel_id");
  }

If nothing is found, it will use the default channel properties baked into the library.

  final private String DEFAULT_CHANNEL_ID = "channel_01";
  final private String DEFAULT_CHANNEL_NAME = "Channel Name";

So in theory, you could just register a channel with this ID and channel name, and it would just work without the other code to decorate the payload. Naturally, that's probably only appropriate for local testing/development.

Hope this is useful!

@dwardin you're a legend mate. Perfect fix 👍

stale[bot] commented 2 years ago

The issue has been closed for inactivity.