OneSignal-Flutter-SDK icon indicating copy to clipboard operation
OneSignal-Flutter-SDK copied to clipboard

setNotificationOpenedHandler is not called when I click the notification and app in killed status #599

Open shivamsharma-1996 opened this issue 2 years ago • 14 comments

What happened?

setNotificationOpenedHandler is not called when I click the notification and app in killed status

Steps to reproduce?

1. Open the application
2. Swipe out the application
3. Trigger a notification from the dashboard
4. Click on the notification
5. The handler is not called

What did you expect to happen?

setNotificationOpenedHandler should work irrespective to the app state.

OneSignal Flutter SDK version

OneSignal Flutter SDK 3.4.1

Which platform(s) are affected?

  • [ ] iOS
  • [X] Android

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

shivamsharma-1996 avatar Nov 09 '22 07:11 shivamsharma-1996

I thought I had the same problem but actually the handler is called (very early).

Here's a snippet of my code:

OneSignal.shared.setNotificationOpenedHandler((openedResult) {
  // called whenever the user opens a OneSignal push notification
  _lastOpenedOneSignalNotification = openedResult.notification;
  if (openedResult.notification.additionalData != null) {
    PiadaNotificationsHandler.defaultKey.currentState
        ?.handleNotificationPayload(openedResult.notification.additionalData!);
  }
});

OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
  // called whenever a notification arrives and the application is in foreground
  event.complete(event.notification);
});

// OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
await OneSignal.shared.setAppId(const String.fromEnvironment("..."));

As you can see I save the _lastOpenedOneSignalNotification to reuse later when another widget is finally loaded and ready to handle the action.

I tested this code on an iPhone XR and a Motorola E4

muccy avatar Nov 10 '22 16:11 muccy

Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work

Davete0302 avatar Nov 21 '22 01:11 Davete0302

i got same problem. i can't find a way to access to additinalData after the app is opened

spirituelle avatar Feb 02 '23 22:02 spirituelle

Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work

does notification work when the app is running?

spirituelle avatar Feb 02 '23 22:02 spirituelle

Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work

does notification work when the app is running?

yes it works. for background try running flutter run --release. I opted to use firebase messaging though

davetebonfire0302 avatar Feb 03 '23 05:02 davetebonfire0302

Hi, how did you setup android to receive notification when app is in background or killed? I can't seem to make it work

does notification work when the app is running?

yes it works. for background try running flutter run --release. I opted to use firebase messaging though

use onesignal instead. it's based on firebase but with more features

spirituelle avatar Feb 03 '23 12:02 spirituelle

I thought I had the same problem but actually the handler is called (very early).

Here's a snippet of my code:

OneSignal.shared.setNotificationOpenedHandler((openedResult) {
  // called whenever the user opens a OneSignal push notification
  _lastOpenedOneSignalNotification = openedResult.notification;
  if (openedResult.notification.additionalData != null) {
    PiadaNotificationsHandler.defaultKey.currentState
        ?.handleNotificationPayload(openedResult.notification.additionalData!);
  }
});

OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
  // called whenever a notification arrives and the application is in foreground
  event.complete(event.notification);
});

// OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
await OneSignal.shared.setAppId(const String.fromEnvironment("..."));

As you can see I save the _lastOpenedOneSignalNotification to reuse later when another widget is finally loaded and ready to handle the action.

I tested this code on an iPhone XR and a Motorola E4

Hi, @muccy. Were you able to access notification data even when app is killed? I think I didn't get the point. In our application we have configured setNotificationOpenedHandler, that works only when the app is already running.

The only part we didn't implemented yet is setNotificationWillShowInForegroundHandler, but I don't think it's responsible for catching the notification data when the app is killed.

ebragaparah avatar Mar 29 '23 14:03 ebragaparah

Hey @ebragaparah, I confirm you the "notification opened handler" is called also when the app is opened (if the app is opened through a push notification activation).

I can only add that my app setup is a but awkward because it also includes Firebase FCM, I don't know if it matters (I had to follow this tip on iOS: https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/348#issuecomment-824163607).

I also remember the handler was called very very early, so make sure you assign it ASAP

muccy avatar Mar 30 '23 11:03 muccy

Thanks, @muccy.

yes, sure! We are already using the setNotificationOpenedHandler, but as I said, it only works when the app is already running or when it's not killed. So, do you have to use FCM in order to have the notification correctly parsed when the app is not running?

and yes, OneSignal is being setup very very early.

ebragaparah avatar Mar 30 '23 11:03 ebragaparah

I don't know if it's a coincidence, but I thought it may be the only difference in our setups

muccy avatar Mar 30 '23 11:03 muccy

right. but, are you able to parse the content of a received push notification when the app is not on background nor foreground? I mean when the state is killed or not started, something like this

ebragaparah avatar Mar 30 '23 12:03 ebragaparah

Yeah. During my tests I killed my app on purpose in order to inspect the incoming payload. To do so I compiled the app in release mode

muccy avatar Mar 30 '23 15:03 muccy

Hey, @muccy! Thanks for all the support, dude! I checked up my setup and realized that I was not providing correctly all the dependencies that the view need.

The OneSignal hook is working properly even when the app is killed, and now I'm able to get the data from the push notification and handle it.

Another interesting thing is that I could visualize the issue running on release mode, in a real device.

ebragaparah avatar Apr 03 '23 14:04 ebragaparah

Great to hear so, dude :)

muccy avatar Apr 03 '23 16:04 muccy