Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get an interactive action response when iOS app is in dead state #724

Closed
mkizesov opened this issue Feb 17, 2021 · 25 comments
Closed
Assignees

Comments

@mkizesov
Copy link

mkizesov commented Feb 17, 2021

Version: [email protected]
System: iOS

  • When the app is not dead (foreground, background) and I tap on one of the actions in notification, then registerNotificationOpened callback is called and I get actionResponse there
  • When the app is dead and I tap on notification (not on action), I get notification from Notifications.getInitialNotification()
  • When the app is dead and I tap on one of the actions in push notification, Notifications.getInitialNotification() returns undefined and registerNotificationOpened callback is not called
import { Notifications, NotificationAction, NotificationCategory } from 'react-native-notifications';

Notifications.registerRemoteNotifications();

Notifications.events().registerNotificationOpened((_notification: Notification, completion, actionResponse) => {
    // this callback is called only when app is not dead
    completion();
});

let acceptAction = new NotificationAction('ACCEPT_ACTION', 'foreground', 'Accept', true);
let skipAction = new NotificationAction('SKIP_ACTION', 'foreground', 'Skip', true);
let someCategory = new NotificationCategory('SOME_CATEGORY', [acceptAction, skipAction]);
Notifications.setCategories([someCategory]);

// This will be undefined when the app is dead and I tap on one of the actions in push notification
const notification = await Notifications.getInitialNotification();

Will appreciate any comments. Thanks!

@kgsachinthaudara
Copy link

@mkizesov Did you checked in real device ?

@mkizesov
Copy link
Author

@mkizesov Did you checked in real device ?

Of course.

@kgsachinthaudara
Copy link

Any possibility share version and code snaps with me, because In my code base not any event get trigger in emulator and real device.

@ninjz
Copy link

ninjz commented Feb 26, 2021

@mkizesov I'm experiencing the same issue. I'm sure that this should be possible on iOS because there are apps out there that can handle this functionality. I'm stumped, been at it for the past 2 days and I've tried react-native-push-notification-ios

From what I've gathered, launchOptions inside application: didFinishLaunchingWithOptions should contain the information from the launching notification. I'm launching the app to the background from dead through the notification action and it returns null. Not sure where this went wrong because that's really the first entry point.

I'm starting to wonder if there's a certain option that needs to be in the notification config or something. I've seen some say they have solved this but I think their case (not confirmed) is when the notification action launches the app into "foreground" from dead.

@mkizesov
Copy link
Author

mkizesov commented Mar 1, 2021

@ninjz As I understand, when app is dead and you tap on action in notification, the native part of the app code works:
didReceiveNotificationResponse called (https://github.com/wix/react-native-notifications/blob/master/lib/ios/RNNotificationEventHandler.m#L31)
I tested it.

But then it tries to call JS callback from your Notifications.events().registerNotificationOpened() (by sending event RNNotificationOpened) and failed. Because by the time when app is started there's no callback registered for this event because it takes time to execute all your js code. So maybe it works in some ideal conditions, but not in a real world.

@mkizesov
Copy link
Author

mkizesov commented Mar 1, 2021

@ninjz So I forked the library and added a function similiar to getInitialNotification but for last action and now it works in any state. When an app is in foreground or background, I get an action in registerNotificationOpened callback.
And an app is launched, I check if there any action response by doing this
const lastAction = await Notifications.getLastAction();
I've found this soultion in one of the closed issues here.

https://github.com/mkizesov/react-native-notifications/commits/master

@ninjz
Copy link

ninjz commented Mar 3, 2021

@mkizesov Thanks for sharing. I've gotten to that stage where I'm able to retrieve the notification from the killed state using a similar method.

However, now I've been testing these actions on the Apple Watch, and sometimes the phone receives the action, sometimes it doesn't – usually after the app has been backgrounded for at least 30 minutes. If I go to the phone and respond to the notification action it is properly handled.

Do you think you can give this a try? I'm wondering if it has to do with React Native because I've made a sample app using native Swift and am able to consistently get callbacks on the didReceiveNotificationResponse handler no matter how long I've left the app for, in killed and background state.

It doesn't make sense for this to not work on the watch properly, as it would result in unexpected behavior by the users thinking these buttons actually do something...

@mp8446
Copy link

mp8446 commented Mar 3, 2021

@mkizesov Thanks for sharing. I've gotten to that stage where I'm able to retrieve the notification from the killed state using a similar method.

However, now I've been testing these actions on the Apple Watch, and sometimes the phone receives the action, sometimes it doesn't – usually after the app has been backgrounded for at least 30 minutes. If I go to the phone and respond to the notification action it is properly handled.

Do you think you can give this a try? I'm wondering if it has to do with React Native because I've made a sample app using native Swift and am able to consistently get callbacks on the didReceiveNotificationResponse handler no matter how long I've left the app for, in killed and background state.

It doesn't make sense for this to not work on the watch properly, as it would result in unexpected behavior by the users thinking these buttons actually do something...

I'm facing the same issue right now. When pressing an action on the iPhone everything works fine and registerNotificationOpened() gets raised. But when clicking on the action on an AppleWatch "nothing" happens. The app get's started (background), but the event doesn't raise.

@ninjz
Copy link

ninjz commented Mar 4, 2021

Really strange. To the point, I'm ripping out every dependency one by one to try and isolate the issue. I've made a fresh new project using only react-native-notification-ios and everything seems to be working fine. I've yet to do the same with wix/react-native-notifications though. Are you using Firebase, Sentry, and/orwix/react-native-navigation by any chance @mp8446 ?

@mp8446
Copy link

mp8446 commented Mar 4, 2021

@ninjz No, I'm not using Firebase, Sentry or wix/react-native-navigation currently.
Here is a list of my dependencies, maybe there is an intersection with yours:

"@react-native-community/checkbox": "^0.5.7",
"@react-native-community/datetimepicker": "^3.0.9",
"@react-native-community/masked-view": "^0.1.10",
"@react-native-picker/picker": "^1.9.10",
"@react-navigation/bottom-tabs": "^5.11.7",
"@react-navigation/native": "^5.9.2",
"@react-navigation/stack": "^5.14.2",
"formik": "^2.2.6",
"react": "16.13.1",
"react-native": "^0.63.4",
"react-native-draggable-flatlist": "^2.5.4",
"react-native-ep-calendar": "^1.0.2",
"react-native-gesture-handler": "^1.9.0",
"react-native-haptic-feedback": "^1.11.0",
"react-native-notifications": "^3.4.2",
"react-native-reanimated": "^1.13.2",
"react-native-restart": "^0.0.20",
"react-native-safe-area-context": "^3.1.9",
"react-native-screens": "^2.17.1",
"react-native-slider": "^0.11.0",
"react-native-sqlite-storage": "^5.0.0",
"react-native-svg": "^12.1.0",
"react-native-toast-message": "^1.4.4",
"yup": "^0.32.8"

@ninjz
Copy link

ninjz commented Mar 5, 2021

Thanks for sharing that @mp8446 . I've given it a try and ripped out all the dependencies that we have in common. It did not seem to resolve the issue.

Finally, I ended up adding a call to PushNotificationIOS.requestPermissions() during app initialization and for some reason all seems to be fine. For now... Funny enough, that even if I comment out the function now, it still works. I've been testing it constantly throughout the past day and it's been going well so far.

I have a theory as to why this is happening: That there might have been a bug with the permission request method in UNUserNotificationCenter on the OS level that ended up being fixed in a recent OS update, and it needed to be called again to fix. Anyways, it's just a theory.

Do you mind giving this a try and seeing if this ends up fixing the issue on your end? I'm going to continue testing this over the next couple of days to see if it holds up.

EDIT:
If you are calling Notifications.registerRemoteNotifications() from the wix library and this is still occurring for you however, I noticed that the main difference between PushNotificationIOS.requestPermissions() and the wix package is the native call to registerForRemoteNotifications.

wix/react-native-notifications:

[[UIApplication sharedApplication] registerForRemoteNotifications];

@react-native-community/react-native-push-notification-ios:

[RCTSharedApplication() registerForRemoteNotifications];

For me it is working with the call from @react-native-community/react-native-push-notification-ios and I can't verify if it's ok on the wix package, because now that I've made the call it seems I can't make this not work for me again.

Anyways, in my app, I exclusively used react-native-permissions to request for permissions, and the call to registerForRemoteNotifications in there is also called on [UIApplication sharedApplication] not sure how RCTSharedApplication works, but if this does actually ONLY works from @react-native-community/react-native-push-notification-ios I'd say it's highly likely this is where the issue is from. Not see why it's relevant though because I'm purely just using local notifications.

@mp8446
Copy link

mp8446 commented Mar 7, 2021

@ninjz Thank you for your advice. But sadly it doesn't work for me. Neither with Notifications.registerRemoteNotifications(), also not with PushNotificationIOS.requestPermissions(). Seems like I have to dig deeper

EDIT:
Nevermind, it worked after a fresh pod install, including the approach by mkizesov. Thank you very much :)

@stale
Copy link

stale bot commented Apr 6, 2021

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

@stale stale bot added the 🏚 stale label Apr 6, 2021
@stale
Copy link

stale bot commented Apr 14, 2021

The issue has been closed for inactivity.

@stale stale bot closed this as completed Apr 14, 2021
@mkizesov
Copy link
Author

The issue is still valid.

@DanielEliraz DanielEliraz reopened this Jun 26, 2021
@stale stale bot removed the 🏚 stale label Jun 26, 2021
@DanielEliraz DanielEliraz self-assigned this Jun 26, 2021
@jamesholcomb
Copy link

@mkizesov would you be willing to submit a PR using the fix from your fork?

@DanielEliraz
Copy link
Collaborator

@mkizesov would you be willing to submit a PR using the fix from your fork?

@mkizesov, indeed it will be appreciated if you'll make a PR, but please first remove the tslint fixes please 🙏

@stale
Copy link

stale bot commented Aug 12, 2021

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

@stale stale bot added the 🏚 stale label Aug 12, 2021
@callaars
Copy link

This issue is still ongoing.

@stale
Copy link

stale bot commented Sep 17, 2021

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

@stale stale bot added the 🏚 stale label Sep 17, 2021
@stale
Copy link

stale bot commented Sep 24, 2021

The issue has been closed for inactivity.

@stale stale bot closed this as completed Sep 24, 2021
@honzajerabek
Copy link

Hi is this going to be addressed? I see there's also a PR which was closed for inactivity. I believe actions not working when app is killed is a valid issue.

Or is there a way how to workaround this with the current version?

@yon-l-10p
Copy link

Facing the same issue as well. is there any workaround?

@stale
Copy link

stale bot commented Sep 16, 2023

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

@stale stale bot added the 🏚 stale label Sep 16, 2023
@stale
Copy link

stale bot commented Oct 15, 2023

The issue has been closed for inactivity.

@stale stale bot closed this as completed Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants