zo0r / react-native-push-notification

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

Android : onNotification not working in local notification #1556

Open carlos-marcello opened 4 years ago

carlos-marcello commented 4 years ago

Hi, how can i acces onNotification in local notification? despite all the advice on the net nothing works

`

<meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
            android:value="localpush"/>
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                android:value="localpush"/>

    <!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground" android:value="true"/>
    <!-- Change the value to false if you don't want the creation of the default channel -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.channel_create_default"
                android:value="true"/>
    <!-- Change the resource name to your App's accent color - or any other color you want -->
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
    <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.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>`
Dallas62 commented 4 years ago

I don't understand what you want.

carlos-marcello commented 4 years ago

onNotification not working

carlos-marcello commented 4 years ago

PushNotification.configure({

  onRegister: function (token) {
    console.log("TOKEN:", token);
  },

  onNotification: function (notification) {

    //alert(JSON.stringify(notification))
    //console.log("NOTIFICATION:", notification);

     if (notification.action === "Take") {
        alert("hey1");
      } else if (notification.action === "Skip") {
         alert("hey2");
      } else if (notification.action === "Snooze") {
         alert("hey3");
      }
  },

  onAction: function (notification) {
    console.log("ACTION:", notification.action);
    console.log("NOTIFICATION:", notification);

  },

  onRegistrationError: function(err) {
    console.error(err.message, err);
  },

   permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  popInitialNotification: true,

  requestPermissions: true,

});

onNotification not working in local push, how can i fix this?

Dallas62 commented 4 years ago

Why do you need this ? Why don't you put the logic when you call PushNotification.localNotification ?

carlos-marcello commented 4 years ago

i need this to cancel the notification

`componentDidMount() {

var db = SQLite.openDatabase({ name:'BuinessFamilyT.db', createFromLocation:'~BuinessFamilyT.db'});

//this.checkCredit(db); this.sendNotification(); this.interval =setInterval(()=>{ this.checkFirstTimeUsage(db); },1800);

this.cancelAll() }

sendNotification = () => { //var notifMessage= echeance+' - '+amount ; // PushNotification.localNotification({ // showWhen: true, // (optional) default: true // autoCancel: true, // (optional) default: true // largeIcon: "ic_launcher", // (optional) default: "ic_launcher" // largeIconUrl: "https://www.example.tld/picture.jpg", // (optional) default: undefined // smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher" // bigPictureUrl: "https://www.example.tld/picture.jpg", // (optional) default: undefined // color: "red", // (optional) default: system default // vibrate: true, // (optional) default: true // vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000 // title: 'notifTitle', // (optional) // message: 'notifMessage', // (required) // playSound: true, // (optional) default: true // soundName: "default", // number:10 // }); };

PushNotification.configure({

  onRegister: function (token) {
    console.log("TOKEN:", token);
  },

  onNotification: function (notification) {

    //alert(JSON.stringify(notification))
    //console.log("NOTIFICATION:", notification);

     if (notification.action === "Take") {
        alert("hey1");
      } else if (notification.action === "Skip") {
         alert("hey2");
      } else if (notification.action === "Snooze") {
         alert("hey3");
      }
  },

  onAction: function (notification) {
    console.log("ACTION:", notification.action);
    console.log("NOTIFICATION:", notification);

  },

  onRegistrationError: function(err) {
    console.error(err.message, err);
  },

   permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  popInitialNotification: true,

  requestPermissions: true,

});

`

Dallas62 commented 4 years ago

I still don't understand but one thing I see is that .configure is inside a component. https://github.com/zo0r/react-native-push-notification#usage First line 😉

carlos-marcello commented 4 years ago

.configure () must be in another file?

Dallas62 commented 4 years ago

Just outside of the component.

carlos-marcello commented 4 years ago

in my file it is outside of the component, what i want to know why onNotification console.log in onNotification return nothing

Dallas62 commented 4 years ago

Can you provide more information ? Such as:

Without information, I'm not able to help.

vishwa1937 commented 4 years ago

I still don't understand but one thing I see is that .configure is inside a component. https://github.com/zo0r/react-native-push-notification#usage First line 😉

Hello @Dallas62 , I am also facing issue that onNotification is not called when app is in background/killed but notification is coming from remote server. It uses default configuration for notification design while in background/killed state. I am not able to add largeIconUrl, bigPictureUrl, color ... etc. in notification .

As you said DO NOT USE .configure() INSIDE A COMPONENT, EVEN App but I am not clear where and how to use PushNotification.configure properly.

I used following format for notification from remote server { "message": { "token":"fcm token", "notification": { "body": "Have a nice day", "title": "Good Morning", }, "data": { "icon": 'ic_notification', "url": "www.google.com", "soundName": "default", } } }

In my case i used it in App.tsx outside the function component. Could you please elaborate on this. Thank you.

carlos-marcello commented 4 years ago

for the first question, the notification is triggered with an event. it is a local notification not a notification with a remote server or firebase for the third question even if the state of the application is background, fireground or kille onNotification does not return information about the notification. For the fourth question yes the user interacts with the notification

Dallas62 commented 4 years ago

I still don't understand but one thing I see is that .configure is inside a component. https://github.com/zo0r/react-native-push-notification#usage First line 😉

Hello @Dallas62 , I am also facing issue that onNotification is not called when app is in background/killed but notification is coming from remote server. It uses default configuration for notification design while in background/killed state. I am not able to add largeIconUrl, bigPictureUrl, color ... etc. in notification .

As you said DO NOT USE .configure() INSIDE A COMPONENT, EVEN App but I am not clear where and how to use PushNotification.configure properly.

I used following format for notification from remote server { "message": { "token":"fcm token", "notification": { "body": "Have a nice day", "title": "Good Morning", }, "data": { "icon": 'ic_notification', "url": "www.google.com", "soundName": "default", } } }

In my case i used it in App.tsx outside the function component. Could you please elaborate on this. Thank you.

Hi @vishwa1937 This case is already answered in others issues:

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

App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system trayData: in extras of the intent.

Both data and notification will not trigger onNotification (background). You can send data-only then trigger the appropriate local notification, or use your current payload and wait for the interaction of the user.

Dallas62 commented 4 years ago

for the first question, the notification is triggered with an event. it is a local notification not a notification with a remote server or firebase for the third question even if the state of the application is background, fireground or kille onNotification does not return information about the notification. For the fourth question yes the user interacts with the notification

onNotification will not be triggered when the local notification pop-up, you can implement the logic you need when you call PushNotification.localNotification. If onNotification is not called when the user press the notification, two reasons:

carlos-marcello commented 4 years ago

ok thanks but what logic I have to put in place when I call PushNotification.localNotification, because it is a little fuzzy for me because according to the documentation it is something like that `PushNotification.localNotification({ / Android Only Properties / id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID ticker: "My Notification Ticker", // (optional) showWhen: true, // (optional) default: true autoCancel: true, // (optional) default: true largeIcon: "ic_launcher", // (optional) default: "ic_launcher" largeIconUrl: "https://www.example.tld/picture.jpg", // (optional) default: undefined smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher" bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop subText: "This is a subText", // (optional) default: none bigPictureUrl: "https://www.example.tld/picture.jpg", // (optional) default: undefined color: "red", // (optional) default: system default vibrate: true, // (optional) default: true vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000 tag: "some_tag", // (optional) add tag to message group: "group", // (optional) add group to message groupSummary: false, // (optional) set this notification to be the group summary for a group of notifications, default: false ongoing: false, // (optional) set whether this is an "ongoing" notification priority: "high", // (optional) set notification priority, default: high visibility: "private", // (optional) set notification visibility, default: private importance: "high", // (optional) set notification importance, default: high allowWhileIdle: false, // (optional) set notification to work while on doze, default: false ignoreInForeground: false, // (optional) if true, the notification will not be visible when the app is in the foreground (useful for parity with how iOS notifications appear) shortcutId: "shortcut-id", // (optional) If this notification is duplicative of a Launcher shortcut, sets the id of the shortcut, in case the Launcher wants to hide the shortcut, default undefined channelId: "your-custom-channel-id", // (optional) custom channelId, if the channel doesn't exist, it will be created with options passed above (importance, vibration, sound). Once the channel is created, the channel will not be update. Make sure your channelId is different if you change these options. If you have created a custom channel, it will apply options of the channel. onlyAlertOnce: false, //(optional) alert will open only once with sound and notify, default: false

actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true

/ iOS only properties / alertAction: "view", // (optional) default: view category: "", // (optional) default: empty string userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error)

/ iOS and Android properties / title: "My Notification Title", // (optional) message: "My Notification Message", // (required) playSound: false, // (optional) default: true soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played) number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero) repeatType: "day", // (optional) Repeating interval. Check 'Repeating Notifications' section for more info. });`

for "that An Activity intercept the intent (splashscreen ?) this case has been answered in issues yesterday and few times ago." I can have the subject link please

Dallas62 commented 4 years ago

You just have to run the code you want to run before or after PushNotification.localNotification

the_logic_you_want_to_run_before();
PushNotification.localNotification( /* your params */ );
the_logic_you_want_to_run_after();  // This is exactly the same as expecting onNotification to be called on notification-trigger.

For intent intercepted: #1550

vishwa1937 commented 4 years ago

I still don't understand but one thing I see is that .configure is inside a component. https://github.com/zo0r/react-native-push-notification#usage First line 😉

Hello @Dallas62 , I am also facing issue that onNotification is not called when app is in background/killed but notification is coming from remote server. It uses default configuration for notification design while in background/killed state. I am not able to add largeIconUrl, bigPictureUrl, color ... etc. in notification . As you said DO NOT USE .configure() INSIDE A COMPONENT, EVEN App but I am not clear where and how to use PushNotification.configure properly. I used following format for notification from remote server { "message": { "token":"fcm token", "notification": { "body": "Have a nice day", "title": "Good Morning", }, "data": { "icon": 'ic_notification', "url": "www.google.com", "soundName": "default", } } } In my case i used it in App.tsx outside the function component. Could you please elaborate on this. Thank you.

Hi @vishwa1937 This case is already answered in others issues:

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

App state Notification Data Both Foreground onMessageReceived onMessageReceived onMessageReceived Background System tray onMessageReceived Notification: system trayData: in extras of the intent. Both data and notification will not trigger onNotification (background). You can send data-only then trigger the appropriate local notification, or use your current payload and wait for the interaction of the user.

@Dallas62 Thank you. As, you said the problem was in notification json format while sending from remote server. I removed notification and sent data only. Now it is working as expected in android. I need to check in iOS also. Will update about iOS also.

We need to put this information in documentation also. Thank you again.

carlos-marcello commented 4 years ago

Hello, it work now but when app is in background i can't receive the notification, i test with the emulator, it's why?

vishwa1937 commented 4 years ago

Hello, it work now but when app is in background i can't receive the notification, i test with the emulator, it's why?

@carlos-marcello May be your data format is not according to firebase. Please check on that. I have tested in foreground, background , killed stage in Android and it is working well in physical device and emulator both. You can see reply above about this answered by @Dallas62 . https://github.com/zo0r/react-native-push-notification/issues/1556#issuecomment-663479218

carlos-marcello commented 4 years ago

Hello, it work now but when app is in background i can't receive the notification, i test with the emulator, it's why?

@carlos-marcello May be your data format is not according to firebase. Please check on that. I have tested in foreground, background , killed stage in Android and it is working well in physical device and emulator both. You can see reply above about this answered by @Dallas62 . #1556 (comment)

even for local notification?

silencer07 commented 4 years ago

Ok i have some inputs here. for some reason android is hanging when requestPermissions is true. what i did is this

requestPermissions: Platform.OS === "ios"

then when sending local notification:

PushNotification.checkPermissions(permissions => { console.log("permissions", permissions) if (permissions.alert === true) { // or check others if you need them // do the PushNotification.localNotification()

in my case if i dont do it like this, the app will hang

rajan-keypress commented 3 years ago

not work the way i want it

tomarviii88 commented 3 years ago

Check if you have created channel or not because without channel notifications won't work now. Checkout this: https://github.com/zo0r/react-native-push-notification#channel-management-android And for more reference you can go through the /example directory of this repository.

gabrielsaviank commented 3 years ago

@silencer07 do you set checkPermissions in the notification.android.js?

Does anyone know if there is a better way to send notifications instead using this library?

Dallas62 commented 3 years ago

Hi @gabrielsaviank

You can use RNFirebase, react-native-notifications or create your own library.

But it would be great if you explain your issue. Saying "I want to avoid this library" will not help to improve this library or help others.

Regards

gabrielsaviank commented 3 years ago

@Dallas62 I fixed. I had to downgrade the version to the 3.1.1. Now is working.

Dallas62 commented 3 years ago

You fixed what ? Downgrading the version will solve nothing. You just get stuck in an old version which doesn't include fixes or doesn't work with some Android Device.

If this is working in old version, it's probably because you didn't have a channel created in latest version.

As @tomarviii88 mentioned:

Check if you have created channel or not because without channel notifications won't work now. Checkout this: https://github.com/zo0r/react-native-push-notification#channel-management-android And for more reference you can go through the /example directory of this repository.

Like he said, it's written in the Readme: NOTE: Without channel, notifications don't work

Regards,

HappyCodingLover commented 3 years ago

@Dallas62 thanks for this awesome lib now, localNotification and ScheduleNotification only gives me vibration, and don't appear. Can you tell me why?

Version: ^7.2.1 RN: 63.3 Google Services 4.3.5 Mobile: Huawei, Android 9.0