Я разрабатываю плагин для родной платформы Flutter и пытаюсь заставить работать с ним уведомления pu sh (через Firebase), но didReceiveRemoteNotification
в плагине никогда не вызывается.
I ' мы добавили addApplicationDelegate
к моему AppDelegate
, и я получаю didReceiveRemoteNotification
, запущенный в AppDelegate
, но не в плагине. Однако я получаю didRegisterForRemoteNotificationsWithDeviceToken
обратных вызовов как в AppDelegate
, так и в плагине. Я использую обмен сообщениями Firebase, который может с этим не справиться, поэтому мне интересно, если бы кто-нибудь попробовал это или мог знать, что вызывает обратный вызов, не вызывается.
Плагин:
public class SomePlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
...
// Add application delegate so that we can receive AppDelegate callbacks
registrar.addApplicationDelegate(instance)
}
}
extension SomePlugin {
public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NSLog("****/ didRegisterForRemoteNotificationsWithDeviceToken")
}
public func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
NSLog("****/ didReceiveRemoteNotification")
}
public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> Bool {
NSLog("****/ didReceiveRemoteNotification")
return true
}
}
AppDelegate:
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self as UNUserNotificationCenterDelegate
// set the type as sound or badge
center.requestAuthorization(options: [.sound,.alert,.badge, .providesAppNotificationSettings]) { (granted, error) in
// Enable or disable features based on authorization
}
application.registerForRemoteNotifications()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
extension AppDelegate {
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NSLog("**** didRegisterForRemoteNotificationsWithDeviceToken")
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
override func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.newData)
super.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
}
}
extension AppDelegate {
// when user opens the notification
override func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
NSLog("**** userNotificationCenter: did receive!!!!")
completionHandler()
super.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
// app is in foreground
override func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("**** userNotificationCenter: will present")
completionHandler(UNNotificationPresentationOptions.sound)
super.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
}
}