OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
625 stars 215 forks source link

OneSignal.Notifications.addClickListener does not fire on push notification tap when app is killed #918

Open nandaprasesoft98 opened 4 months ago

nandaprasesoft98 commented 4 months ago

How can we help?

Hi,

I just reopen this issues (https://github.com/OneSignal/OneSignal-Flutter-SDK/issues/723) that in 5.2.2 is still same issue in original firebase, using FirebaseMessaging.onMessageOpenedApp.listen, if in background mode and clicking notification, the listener callback is called. But if in one signal (OneSignal.Notifications.addClickListener), the listener callback is not called

Anyone who helps solve this problem, I would like to thank you very much

Code of Conduct

MayZarHlaing commented 4 months ago

I also face that kinds of issue on 5.2.0 . Please help me out!

ihijazi commented 3 months ago

Been having similar behavior, not only on cold start.

My app uses some native code for some other SDK. The other SDK had GeneratedPluginRegistrant.register(with: "xxx"), and it was placed AFTER GeneratedPluginRegistrant.register(with: self).

The GeneratedPluginRegistrant.register(with: self) has to be at the very end, for both Android and iOS.

example for swift:

        GeneratedPluginRegistrant.register(with: xxx)

        GeneratedPluginRegistrant.register(with: self) // this has to be at the very end

        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

Tested with latest 5.2.2 and working fine.. all handlers as expected.

Hope this helps someone.

Praveena0989 commented 3 months ago

Solution given by @ihijazi should work for most of the people, but the problem is home_widget plugin which registers background callback for interactive widget on iOS 17 asynchronously, which is causing the problem for onesignal. This is the problem

  if #available(iOS 17, *) {
    HomeWidgetBackgroundWorker.setPluginRegistrantCallback { registry in
     GeneratedPluginRegistrant.register(with: registry)
     }
   }

To fix this, I am re-registering onesignal plugin after setting the background callback for home_widget.

private func reregisterOneSignal(){
        OneSignalPlugin.register(with: self.registrar(forPlugin: "OneSignalPlugins")!)
        print("OneSignalPlugin reregistered")
    }

Calling it in flutter like this

await HomeWidget.registerInteractivityCallback(backgroundCallback);
await platform.invokeMethod<bool>('reregisterOneSignal');
vdiaza commented 3 months ago

I am comfronting same issue, and @ihijazi solution did not work for me 😓

My appDelegate looks like this

import Flutter
import UIKit
import flutter_background_service_ios
import FirebaseCore
import app_links

@main
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    SwiftFlutterBackgroundServicePlugin.taskIdentifier = "id.flutter.flutter_background_service.BackgroundService"

    if #available(iOS 10.0, *) {
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }

    GeneratedPluginRegistrant.register(with: self)

    // Retrieve the link from parameters
    if let url = AppLinks.shared.getLink(launchOptions: launchOptions) {
      // We have a link, propagate it to your Flutter app or not
      AppLinks.shared.handleLink(url: url)
      return true // Returning true will stop the propagation to other packages
    }

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  override func application(
    _ application: UIApplication,
    continue userActivity: NSUserActivity,
    restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
  ) -> Bool {
    if let incomingURL = userActivity.webpageURL {
      // Handle the Universal Link in Flutter
      AppLinks.shared.handleLink(url: incomingURL)
      return true  // Indicate that the URL has been handled
    }
    return false
  }
}