ios icon indicating copy to clipboard operation
ios copied to clipboard

addEventListener('notification', ...) is not working - when tapping the notification while the app in the background

Open esmatnawahda1 opened this issue 4 years ago • 42 comments

Bug

I'm trying to use the module and this is what happens:

  1. register event listener is working
PushNotificationIOS.addEventListener('register', token => {
        storageManager.storeDeviceToken(token)
    })
  1. getInitialNotification works for me too
PushNotificationIOS.getInitialNotification().then(payload => { ....
....
 payload.finish(PushNotificationIOS.FetchResult)
        }
    })

3. 'notification' event listener is not working

PushNotificationIOS.addEventListener('notification', this.iOSNotificationReceived.bind(this))
...
...
iOSNotificationReceived(data) {
        console.log(data)
    }

I'm not receiving anything here, it is very strange since I implemented all the needed instructions in documentations and the register is working and I'm receiving notifications to my device when the device is killed using getInitialNotification.

Environment info

react-native info output: "react": "16.9.0", "react-native": "0.61.5",

 // paste it here

Library version: x.x.x "@react-native-community/push-notification-ios": "^1.1.1"

Steps To Reproduce

  1. register event listener is working
PushNotificationIOS.addEventListener('register', token => {
        storageManager.storeDeviceToken(token)
    })
  1. getInitialNotification works for me too
PushNotificationIOS.getInitialNotification().then(payload => { ....
....
 payload.finish(PushNotificationIOS.FetchResult)
        }
    })

3. 'notification' event listener is not working

PushNotificationIOS.addEventListener('notification', this.iOSNotificationReceived.bind(this))
...
...
iOSNotificationReceived(data) {
        console.log(data)
    }

Describe what you expected to happen:

To receive notifications when the app is working on the background using PushNotificationIOS.addEventListener('notification', ....)

Reproducible sample code

esmatnawahda1 avatar Apr 25 '20 08:04 esmatnawahda1

It's not a bug - js code is never run in the app background. I'll receive notification in this listener once you tap the notification and the app is moved to foreground.

dominiczaq avatar Apr 25 '20 09:04 dominiczaq

@dominiczaq yes, I know about this, I'm tapping in the notification and not receiving anything inside the 'notification' event listener, I added a console log to print the payload when tapping the notification and moving to foreground and nothing happened.

esmatnawahda1 avatar Apr 25 '20 09:04 esmatnawahda1

@dominiczaq do you have any solution for the issue, it is happening when I tap the notification while the app in the background and then I just arrive to the home page and nothing happens. I worked in the react-native library before and was able to catch the payload when I tap on the notification inside the 'notification' handler.

Will be happy for your help

esmatnawahda1 avatar Apr 25 '20 10:04 esmatnawahda1

Tbh it works fine for me for remote notifications. I believe I've seen some issues opened with local notifications - maybe that's your case as well?

z poważaniem Grzegorz Dominiczak

On Sat, 25 Apr 2020, 12:11 esmatnawahda1, [email protected] wrote:

@dominiczaq https://github.com/dominiczaq do you have any solution for the issue, it is happening when I tap the notification while the app in the background and then I just arrive to the home page and nothing happens. I worked in the react-native library before and was able to catch the payload when I tap on the notification inside the 'notification' handler.

Will be happy for your help

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/react-native-community/push-notification-ios/issues/107#issuecomment-619355459, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWFL7NUIMMHXJV4AS6UNDTROKZMFANCNFSM4MQVAI6A .

dominiczaq avatar Apr 25 '20 10:04 dominiczaq

Thanks @dominiczaq

But no, I mean when I receive a notification by APNs while the app is running in the background and tap on the notification to open the app. When I tap on the notification, the app opens and the nothing happens then.

I tried to remove all the code inside the notification event listener and added an alert and console log to understand if it enters the scope or not, but unfortunately I don't see anything in the terminal and no alert.

I hope this was clear.

import PushNotificationIOS from "@react-native-community/push-notification-ios"

// Utils
import { storageManager } from './'

const handlePushNotifications = (getShareCode, setNotificationRead, navigation) => {
    // PushNotificationIOS.removeDeliveredNotifications(identifiers)
    PushNotificationIOS.getInitialNotification().then(payload => {
        alert('new notification')
        console.log(payload)
    })
    PushNotificationIOS.addEventListener('notification', function (payload) {
        alert('BG notification')
        console.log(payload)
}

const storeIOSToken = () => {
    PushNotificationIOS.requestPermissions()
    PushNotificationIOS.addEventListener('register', token => {
        storageManager.storeDeviceToken(token)
    })
    PushNotificationIOS.addEventListener('registrationError', err => { console.log(JSON.stringify(err)) })
}

export {
    handlePushNotifications,
    storeIOSToken
}
  • storeIOSToken is working

  • PushNotificationIOS.getInitialNotification().then(payload => { is also working when the app is killed

  • PushNotificationIOS.addEventListener('notification', function (payload) { ------> not working

esmatnawahda1 avatar Apr 25 '20 10:04 esmatnawahda1

@dominiczaq any thoughts?

esmatnawahda1 avatar Apr 26 '20 11:04 esmatnawahda1

@esmatnawahda1 is correct experiencing the same thing here. you have sample code to show that it is working?

myusuf3 avatar Apr 30 '20 00:04 myusuf3

@esmatnawahda1 you find anything?

myusuf3 avatar Apr 30 '20 06:04 myusuf3

@esmatnawahda1 is correct experiencing the same thing here. you have sample code to show that it is working?

@myusuf3 no, still struggling with this

esmat-nawahda avatar Apr 30 '20 08:04 esmat-nawahda

i did have some success with switching it to 'localNotification' and then piping the backend to the update. There is an issue #102 that discusses this.

myusuf3 avatar Apr 30 '20 23:04 myusuf3

@dominiczaq updates here?

myusuf3 avatar Apr 30 '20 23:04 myusuf3

Also broken over here. Worst thing is that I spent a considerable amount of time trying to determine if it was an issue in my end, but it seems several people is getting the same. Any clue about what might be wrong?, is the sample application working with remote notifications?

c0d3m3nt0r avatar May 09 '20 05:05 c0d3m3nt0r

Having the same issue here. Also tried for hours, combed through all the examples, run through the setup several times. Just won't work. Get the APN token - so the register event is firing but none of the notification events fire if I tap on a remote notification. All I want to be able to do is know when the user activated the app by tapping a notification and get the details of that notification.

mtettmar avatar May 09 '20 15:05 mtettmar

Solved my issue. didReceiveRemoteNotification was firing but didReceiveNotificationResponse of UserNotificationCenter delegate was never firing when tapping the notification. Note that it WAS firing for a local notification created by the app, but NOT a remote push notification.

I've just spent several days trying everything I can think of to figure out how to detect when that remote notification was tapped and get the payload. In the end what fixed it was changing this in AppDelegate.m:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self; 

To:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)completionHandler:^(BOOL granted, NSError * _Nullable error) {
         if (granted) {
            center.delegate = self; 
          }
 }];

Basically setting center.delegate after requestAuthorizationWithOptions then changed the behaviour.

Now, didReceiveNotificationResponse fires when the remote notification is tapped. Somewhat counter intuitively it is treated as a localNotification (but I guess that makes sense) so I handle it in:

PushNotificationIOS.addEventListener('localNotification' ...

Note that for didReceiveRemoteNotification to also fire, content-available: 1 must be set in the payload. Either way didReceiveNotificationResponse was not firing until making the above change.

Hope this helps someone. I had been pulling my hair out for about 4 days!

mtettmar avatar May 12 '20 14:05 mtettmar

@mtettmar Thanks so much for sharing, your change fixed background and foreground notification touches for me.

Warning foreground notification on touch is always userInteraction: false. Perhaps this is another issue but I've only gotten foreground notifications to fire on touch with @mtettmar changes. 😅

(also Im using this with https://github.com/zo0r/react-native-push-notification)

cogell avatar May 12 '20 14:05 cogell

Hi there! Have a question. If app in foreground and notification received - how can I achieve firing an event? I need to set badge. What have i missed?

akhlopyk avatar May 15 '20 14:05 akhlopyk

@akhlopyk did https://github.com/react-native-community/push-notification-ios/issues/107#issuecomment-627365701 changes work for you? from there you can tie into either:

  • https://github.com/react-native-community/push-notification-ios#addeventlistener OR
  • if using https://github.com/zo0r/react-native-push-notification see https://github.com/zo0r/react-native-push-notification/blob/a7991619d2dd681794ba8035785dc7991ff58eda/example/NotificationHandler.js#L4

cogell avatar May 15 '20 15:05 cogell

@cogell not sure if that what I need.

The thing is - I need to have event handler when app in foreground and notification received. Now event fires only if I click on notification ( notification and localNotification handlers works well in this case).

Thanks for help!

akhlopyk avatar May 15 '20 15:05 akhlopyk

@akhlopyk oh yea, I understand now. that is NOT working for me with this https://github.com/react-native-community/push-notification-ios/issues/107#issuecomment-627365701 work around. sorry.

cogell avatar May 15 '20 15:05 cogell

@akhlopyk - with app in foreground willPresent is fired before the notification appears, so you could modify AppDelegate to use that. According to the docs you pass UNNotificationPresentationOptionNone in the completion handler to silence the notification.

However, I'm using firebase messaging to handle foreground messages - using data messages - where I want to handle things silently without the user having to tap on a notification.

mtettmar avatar May 15 '20 16:05 mtettmar

@mtettmar I have done configuration according to readme of this repo. I'm not new at react-native, and not good at swift. So, If you just let me know what I have to change - I will appreciate!

AppDelegate

akhlopyk avatar May 19 '20 07:05 akhlopyk

Event notification only work when your app open. If you want to get data by tapping notification on background, you can use PushNotificationIOS.getInitialNotification. But it only work on release build, not working on Debug mode. If you want to get data by tapping notification on foreground, you can use PushNotificationIOS.addEventListener('localNotification', cb)

bavuongvng avatar May 25 '20 09:05 bavuongvng

After updating to 1.2.0 I have similar issues - not receiving notifications from 'notification' event.

I've tried listening on 'localNotification' event as @mtettmar suggested (https://github.com/react-native-community/push-notification-ios/issues/107#issuecomment-627365701) and behold, my notifications started appearing again.

After some digging I believe changes from https://github.com/react-native-community/push-notification-ios/commit/15d1085dc73d4defef2fcf1321db37fb67b78053#diff-501451de6931154229be18b351eb277d were the culprit. Now all notifications are coming through didReceiveNotificationResponse:withCompletionHandler which calls localNotificationReceived event in RNCPushNotificationIOS.m (https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions?language=objc - didReceiveNotificationResponse:withCompletionHandler is fired on push tap)

So in summary I believe you would only receive notifications from 'localNotification' event for both remote and local pushes.

dominiczaq avatar May 26 '20 12:05 dominiczaq

@dominiczaq , can you help me please?

The thing is - I need to have event handler when app in foreground and notification received. Now event fires only if I click on notification ( notification and localNotification handlers works well in this case).

Thanks for help!

akhlopyk avatar May 26 '20 13:05 akhlopyk

@mtettmar your comment fixes the issue for me, however, this change results in my app requesting push notification permissions on app launch, rather than waiting until a user logs in (which is the expected behavior in the app). Is there a way to achieve the same result without forcing the request for push permissions on app launch? I'm not that familiar with iOS-specific code, so having a little trouble figuring it out. Seems like maybe didRegisterForRemoteNotificationsWithDeviceToken would be the right place for this?

sam1463 avatar Aug 07 '20 15:08 sam1463

@sam1463 have you managed to find a solution to it? I'm having similar situation as per your description. App is requesting permission after user logged in.

TommyLeong avatar Sep 09 '20 08:09 TommyLeong

@TommyLeong I did not find a solution using this library. I ended up not using this library for push notification handling, using @react-native-firebase/messaging instead

sam1463 avatar Sep 09 '20 13:09 sam1463

@sam1463 At least for us, @react-native-firebase/messaging was anyway part of the lib. Underlying we are using the Firebase service. Anyway, would you be able share how you have done it with @react-native-firebase/messaging ?

TommyLeong avatar Sep 09 '20 13:09 TommyLeong

@mtettmar is there an alternate workaround for this that won't pester the user on initial app load? I am having the same issue as @sam1463 and @TommyLeong are having and am still looking for an alternate workaround for this poor ux.

jSkrills avatar Sep 29 '20 14:09 jSkrills

I am also facing the same issue as mentioned in https://github.com/react-native-push-notification-ios/push-notification-ios/issues/107#issuecomment-629329771. Is there any update / work around?

nattri avatar Nov 30 '20 13:11 nattri