zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.76k stars 2.05k forks source link

Notification on android device not showing in any state(background/foreground/killed), although onNotification is called #1539

Open Reenagrg100 opened 4 years ago

Reenagrg100 commented 4 years ago

Bug

Environment info

react-native info output: react-native:0.62.2 firebase-messaging: 20.2.1 and SNS

Library version: 3.1.9> &&<=4.0.0

Steps To Reproduce

  1. Integrate Push Notification in your app
  2. Trigger a Push notification from FCM console, it worked
  3. Trigger a Push notification from SNS console with default payload (in GCM format)
  4. onNotification will be called, notification will be received in callback but notification won't be shown on your device

Reproducible sample code

export default class PushNotificationController extends Component { constructor(props) { super(props); PushNotification.configure({ // (optional) Called when Token is generated (iOS and Android) onRegister: function(token) { storeObjInPersistentStore('notificationToken', token).then(); },

  // (required) Called when a remote or local notification is opened or received
  onNotification: notification => {
    if (Platform.OS === 'ios' && notification.foreground) {
      showNotificationAlert(
        notification.alert,
        notification,
        handleNotificationTap,
      );
    } else if (!notification.foreground) {
      handleNotificationTap(notification);
    }
  },

  // ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
  senderID:MY_FCM_SENDER_ID,

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

  /**
   * (optional) default: true
   * - Specified if permissions (ios) and token (android and ios) will requested or not,
   * - if not, you must call PushNotificationsHandler.requestPermissions() later
   */
  requestPermissions: true,
});

}

render() { return null; } }

I remember I tried the same using 3.5.2 version on 7th July and it was working using same codebase. But stop working after that. I scratched my head for 4-5 days and at last what I got is that the issue is with version, it started working fine in version 3.1.9 without any further change. I also checked with some other versions but none of them worked in my case.

But I'm not convinced with the solution yet that I got. I request the community to please look at this issue and provide a clear picture of that. Note: I have already tried it with the latest version 4.0.0 but this also didn't work for me.

Dallas62 commented 4 years ago

Hi @Reenagrg100 Can you share the SNS payload you are sending?

Reenagrg100 commented 4 years ago

@Dallas62 This is the payload which I used for sending a message from SNS console. This is the default GCM format. { "GCM": "{ \"data\": { \"message\": \"Sample message for Android endpoints\" } }" }

FYI: I tried with other variations of payload as well.

Dallas62 commented 4 years ago

This is because this kind of payload (data-only) are ignored for popping up to the user. This kind of issue with solution has already been discussed.

Dallas62 commented 4 years ago

1520

Reenagrg100 commented 4 years ago

@Dallas62 But as I already said this should not be the case as I tried it with other payloads also. Let me share those as well. Screenshot 2020-07-18 at 12 16 04 AM

{ "GCM": "{ "data": { "message": "Sample message for Android endpoints" },"userInteraction":false,"foreground":true }" } This is the another one, that I tried and this is the same mentioned in this library doc for android.

But, please answer me one thing as per your answer, how the same payload is working in 3.1.9 version.

Reenagrg100 commented 4 years ago

@Dallas62 In 3.1.9 version everything is working smoothly without modifying anything( even same payload) and I'm receiving notification in killed state too :)

Dallas62 commented 4 years ago

This is an unexpected breaking change introduced in 3.3.0 https://github.com/zo0r/react-native-push-notification/blob/master/CHANGELOG.md#330---2020-04-29

Reenagrg100 commented 4 years ago

@Dallas62 thanks for sharing this. But, I'm still not convinced with it. As I have lots of confusion going on in my mind.

1) As per you, GCM format is removed in 3.3.0, but I tried with GCM format in 3.2.0 version but even it's not working there. 2) I tried with other formats also( not GCM), it's still not working there => In versions above than 3.1.9 This is the payload, I already shared this above. https://user-images.githubusercontent.com/59276080/87820541-e10daa80-c88b-11ea-9c86-6a7c37746fb2.png 3) FYI with the above-given format ( mentioned in point 2), I'm pretty much sure that it was working around 7th July but stop working after that.

It would be great if you can clear all those points. Thanks in advance!!

Dallas62 commented 4 years ago

Hi, This kind of payload (GCM) will not be supported anymore, there is also example in other issues to get a solution. You can stay in an older version 3.1.9, if this is working. If it was working before 7th, it's probably because the dependencies have been updated.

Reenagrg100 commented 4 years ago

@Dallas62 Thanks for the reply:) Could you please share the expected payload for the latest version in the case of SNS?

Dallas62 commented 4 years ago

Hi, I already put a link to an issue that explain how to handle this changes 😉

Reenagrg100 commented 4 years ago

Hey, It would be just great if you can share that link or that issue's link. I searched for it but didn't find it yet.

Dallas62 commented 4 years ago

https://github.com/zo0r/react-native-push-notification/issues/1539#issuecomment-660281745

kyytiPetteri commented 4 years ago

Running into the same type issues where onNotification is called nicely when app is foregrounded and backgrounded. However no notifications are shown at any state (killed, fore- and background). Looking into the native logs I am getting the following errors:

Killed state: Cannot send to notification centre because there is no 'message' field in:... Even thought looking into the bundle the message field clearly is there

Background state:

Screenshot 2020-07-21 at 10 06 49

Foreground: Doesnt really matter since we are implementing in app notifications

Running v4.0.0, RN 0.62.2

Dallas62 commented 4 years ago

Hi @kyytiPetteri As the error says, you are giving a string which is not an Integer. Change it to a real integer, this should solve this issue.

kyytiPetteri commented 4 years ago

@Dallas62 Yeah actually this seems to be an issue of Firebase messaging and this lib. Messageid's from firebase come in completely different format than PushNotification lib gets

Dallas62 commented 4 years ago

Are you sure of that ? I mean, this was tested with Firebase remote messaging few days ago (every release), and you are currently the only one reporting this. I'm not sure Firebase will make this kind of breaking change in few days.

Probably the payload send is not right.

Reenagrg100 commented 4 years ago

Hey @kyytiPetteri how are you triggering the notifications? As in my. case I was doing it from SNS console on AWS side. and what type of payload you are sending?

kyytiPetteri commented 4 years ago

Our server-side is triggering these using https://www.npmjs.com/package/node-pushnotifications.

I fully removed message handling on the Firebase lib and app being in quit state seems to be causing the biggest problems. When sending test notifications through Postman, the native logs give following:

Here is the bundle the that the lib receives:

Bundle[{
  userInteraction=false, 
  id=-423922623,
  foreground=false, 
  data=Bundle[{
    content-available=1, 
    priority=high, 
    body=This is test order status changed message. And it's really really long. This takes forever., 
    icon=<icon_url>, 
    sound=default, 
    title=Test notification,
    message=This is test order status changed message. And it's really really long. This takes forever.
  }]
}];
11:51:24.229    RNPushNotification  onMessageReceived: <Bundle from above>
11:51:24.235    RNPushNotification  sendNotification: <Bundle from above>
11:51:24.237    RNPushNotification  Cannot send to notification centre because there is no 'message' field in:  <Bundle from above>
LightningNemesis commented 3 years ago

@kyytiPetteri I was facing the same issue when the app was in the 'killed' or quit state, it wouldn't receive any notifications. I rectified this issue by making these changes:

1) Make sure you setup your custom channel correctly, somewhat in this manner: PushNotification.createChannel( { channelId: 'MyChannel', channelName: 'PushChanelAlert', vibrate: true, soundName: 'notif', }, (created) => console.log(createChannel returned '${created}'), ); I had called this function inside my App.js

2) Use the correct channel id inside you AndroidManifest.xml (and avoid usage of deprecated methods/configuration entries):

`<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground" android:value="false"/> <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="MyChannel" tools:replace="android:value" /> <meta-data android:name="com.dieam.reactnativepushnotification.notification_color" android:resource="@color/white"/>

  <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" />
      <action android:name="android.intent.action.QUICKBOOT_POWERON" />
      <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
    </intent-filter>
  </receiver>

` 3) Lastly, make sure you are using the same custom notification channel id inside your notification handler:

PushNotification.localNotification({ channelId: 'MyChannel', priority: 'high', // (optional) set notification priority, default: high largeIcon: 'ic_launcher', // (optional) default: "ic_launcher". Use "" for no large icon. smallIcon: 'ic_launcher', ignoreInForeground: false, autoCancel: false, vibrate: true, // (optional) default: true vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000 title: response !== null ? response.data.title : 'Open App for more info!', message:You have recieved a new order!, playSound: true, soundName: 'notif.mp3', });

Upside is, I receive notifications now in the QUIT state, downside is, I'm yet to receive the response object when the app is in killed state, working on that as of now... :)

edwinwong90 commented 3 years ago

Bug

Environment info

react-native info output: react-native:0.62.2 firebase-messaging: 20.2.1 and SNS

Library version: 3.1.9> &&<=4.0.0

Steps To Reproduce

  1. Integrate Push Notification in your app
  2. Trigger a Push notification from FCM console, it worked
  3. Trigger a Push notification from SNS console with default payload (in GCM format)
  4. onNotification will be called, notification will be received in callback but notification won't be shown on your device

Reproducible sample code

export default class PushNotificationController extends Component { constructor(props) { super(props); PushNotification.configure({ // (optional) Called when Token is generated (iOS and Android) onRegister: function(token) { storeObjInPersistentStore('notificationToken', token).then(); },

  // (required) Called when a remote or local notification is opened or received
  onNotification: notification => {
    if (Platform.OS === 'ios' && notification.foreground) {
      showNotificationAlert(
        notification.alert,
        notification,
        handleNotificationTap,
      );
    } else if (!notification.foreground) {
      handleNotificationTap(notification);
    }
  },

  // ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
  senderID:MY_FCM_SENDER_ID,

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

  /**
   * (optional) default: true
   * - Specified if permissions (ios) and token (android and ios) will requested or not,
   * - if not, you must call PushNotificationsHandler.requestPermissions() later
   */
  requestPermissions: true,
});

}

render() { return null; } }

I remember I tried the same using 3.5.2 version on 7th July and it was working using same codebase. But stop working after that. I scratched my head for 4-5 days and at last what I got is that the issue is with version, it started working fine in version 3.1.9 without any further change. I also checked with some other versions but none of them worked in my case.

But I'm not convinced with the solution yet that I got. I request the community to please look at this issue and provide a clear picture of that. Note: I have already tried it with the latest version 4.0.0 but this also didn't work for me.

@Reenagrg100 Here is my finding. The payload you sent is using data property which is silent notification. I guess you need noisy notification you should declare as notification property

Example:

noisy notification
{
"GCM": "{ "notification": { "message": "Sample message for Android endpoints" } }"
}

silent notification
{
"GCM": "{ "data": { "message": "Sample message for Android endpoints" } }"
}

you may use both
{
"GCM": "{ "data": { "message": "Sample message for Android endpoints" }, "notification": { "message": "Sample message for Android endpoints" } }"
}
vishal1Singh commented 2 years ago

Use this payload as a custom payload in AWS SSN publishing : { "GCM":"{ "notification": { "body": "Sample message for Android endpoints", "title":"TitleTest" } }" }