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

[Bug]: Push Notifications Not Opening App When Closed

Open bahadirkalay opened this issue 1 year ago • 8 comments

Description: I am experiencing an issue with push notifications in my Flutter application. When the app is closed, tapping on a push notification does not open the app or navigate to the specified page. Here are the details: Platform:Andoid, IOS Flutter Version: 3.19.0 OneSignal SDK Version: 5.1.2 Expected Behavior: When a push notification is tapped, the app should open and navigate to the specified route or page. Actual Behavior: Tapping on a push notification does not open the app when it is closed.

 void _handleNotificationClick(OSNotificationClickEvent event) async {
    String? deepLink = event.notification.launchUrl;
    String? route = event.notification.additionalData?['route'];
    if (deepLink != null && deepLink.isNotEmpty) {
      Uri url = Uri.parse(deepLink);
      if (await canLaunchUrl(url)) {
        await launchUrl(url, mode: LaunchMode.externalApplication);
      }
    } else if (route != null && route.isNotEmpty) {
      if (navigatorKey.currentState != null) {
        navigatorKey.currentState?.pushNamed(route);
      }
    }
  }

Steps to reproduce?

1:Close the app completely.
2:Send a push notification with a deep link or route.
3:Tap on the notification.
4:Observe that the app does not open or navigate to the intended page.

What did you expect to happen?

I expected the app to open and navigate to the specified page when I tap on a push notification, even if the app is closed.

OneSignal Flutter SDK version

5.1.2

Which platform(s) are affected?

  • [x] iOS
  • [x] Android

Relevant log output


Code of Conduct

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

bahadirkalay avatar Feb 03 '25 06:02 bahadirkalay

Hi @bahadirkalay, is this an issue with the push notification and OneSignal SDK, or is this an issue with your custom navigation?

If you don't use this _handleNotificationClick method, and click on a notification with or without a launch URL, does your app open?

nan-li avatar Feb 11 '25 03:02 nan-li

Hi @bahadirkalay, is this an issue with the push notification and OneSignal SDK, or is this an issue with your custom navigation?

If you don't use this _handleNotificationClick method, and click on a notification with or without a launch URL, does your app open?

Hi @nan-li,

Thank you for your response!

I tested the behavior without using the _handleNotificationClick method, and unfortunately, the app still does not open when the push notification is tapped while the app is closed. This issue occurs on both iOS and Android platforms.

It seems like the app is not being launched at all when tapping the notification, regardless of whether a launch URL is present. When the app is in the background or foreground, the notifications work as expected, but this issue specifically happens when the app is fully closed.

I have followed the setup documentation for OneSignal and ensured that all required permissions and configurations are in place. Do you have any suggestions on how to debug or resolve this issue?

Thank you in advance for your help!

bahadirkalay avatar Mar 10 '25 07:03 bahadirkalay

I am facing the same issue! I am on 5.3.0 and clicking notifications on background and terminated state doesn't opens up the application! And even if I send a a custom link via launchURL it doesn't open that url.

satyam-dev avatar Apr 08 '25 22:04 satyam-dev

and its persistent in version 5.3.3. anyone found solution?

aharissBI avatar May 30 '25 12:05 aharissBI

@bahadirkalay not sure what is happening for you.

  1. To confirm, if you remove the Notification click listener, it still does not launch the app?
  2. And this is on both iOS and Android?
  3. What if you don't include links in the notification? Could something else in your app be blocking the app from opening?

nan-li avatar May 30 '25 21:05 nan-li

@satyam-dev and @aharissBI can you share more details about your issue?

nan-li avatar May 30 '25 21:05 nan-li

@nan-li , please find below more info:

Flutter Environment

[✓] Flutter (Channel stable, 3.32.0, on macOS 15.5 24F74 darwin-arm64, locale en-LB) [302ms]
    • Flutter version 3.32.0 at /Users/ahariss/development/flutter
    • Framework revision: be698c48a6 (2025-05-19)
    • Engine revision: 1881800949
    • Dart version: 3.8.0
    • DevTools version: 2.45.1

Dependencies

  • onesignal_flutter: ^5.3.3
  • auto_route: ^9.2.0
  • get_it: ^7.6.0

Issue Summary

Q: If you remove the notification click listener, does the app still launch?
A: Yes, the app still launches.

Q: Does this happen on both iOS and Android?
A: The issue occurs only on Android when the app is closed using the back button.

Observed Behavior

App State Notification Click App Launches onClickListener Triggered
Killed ✅ Yes ✅ Yes ✅ Yes
In Background ✅ Yes ✅ Yes ✅ Yes
In Foreground ✅ Yes ✅ Yes ✅ Yes
Closed via Back Button ✅ Yes ✅ Yes ❌ No

Q: What if you exclude links from the notification?
A: Same behavior persists.


App Setup Notes

  • Using AutoRoute for navigation.
  • App has a main App class that sets a Dart splash screen, followed by a MainNavigationScreen.
  • The notification click listener is attached in MainNavigationScreen.initState().
  • OneSignal is initialized using a @PostConstruct lifecycle hook in a service registered via get_it.
@PostConstruct()
@override
Future<void> init() async {
  OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
  OneSignal.Notifications.requestPermission(true);

  playerId = await OneSignal.User.getOnesignalId();
  deviceToken = OneSignal.User.pushSubscription.token;

  OneSignal.User.pushSubscription.addObserver((stateChanges) {
    deviceToken = stateChanges.current.token;
  });
}

aharissBI avatar Jun 02 '25 08:06 aharissBI

@nan-li, in addition to the above:


Additional Behavior

If I open a notification that redirects to a detail screen, then:

  1. I press back, which takes me to the MainNavigationScreen.
  2. I then close the app using the back button.
  3. Later, I open the app again via the Recent Apps screen.

👉 The app unexpectedly redirects back to the same detail screen — as if I had clicked the notification again, and sometimes it load different details from old notification.

This only happens when the app was launched via notification.
If I open the app normally and navigate to the same detail screen, then back to MainNavigationScreen and close via the back button, reopening via Recent Apps correctly restores the previous state.

Expected behavior:
When reopening the app via Recent Apps, it should restore the last visible screen (e.g., MainNavigationScreen), not retrigger the notification navigation flow.

aharissBI avatar Jun 02 '25 08:06 aharissBI

@nan-li any update on the above issue?

aharissBI avatar Nov 17 '25 10:11 aharissBI