简体   繁体   中英

Not Receive FCM Push notification in IOS.While app in background or terminate.I m use FirebaseMessaging 6.0.9 Pub

I m receive Fcm Push Notification in the foreground only.app not receive a notification while the app in the background and terminate .

Swift Code

AppDelegate.swift

import UIKit import Flutter import GoogleMaps import Firebase import FirebaseMessaging import FirebaseInstanceID

@UIApplicationMain @objc class AppDelegate: FlutterAppDelegate {

/// didFinishLaunchingWithOptions
override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
    FirebaseApp.configure()
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }
    let remoteNotif = (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [NSObject : AnyObject])
    if remoteNotif != nil {
        self.application(application, didReceiveRemoteNotification: remoteNotif!)
        UIApplication.shared.applicationIconBadgeNumber = 0
        self.window?.makeKeyAndVisible()
        return true
    }
    GMSServices.provideAPIKey("")
    GeneratedPluginRegistrant.register(with: self) //ragister plugin
    application.registerForRemoteNotifications() //register remoteNotifications
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

 /// didRegisterForRemoteNotificationsWithDeviceToken
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Auth.auth().setAPNSToken(deviceToken, type: .unknown)
    Messaging.messaging().apnsToken = deviceToken as Data
}

 /// didReceiveRemoteNotification
override func application(_ application: UIApplication,
                          didReceiveRemoteNotification notification: [AnyHashable : Any],
                          fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if Auth.auth().canHandleNotification(notification) {
        completionHandler(.noData)
        return
    }
    print(notification)
}
override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Unable to register for remote notifications: \(error.localizedDescription)")
}


override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]){
    print("Murtuza")
}
override func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
    if Auth.auth().canHandle(url) {
        return true
    }
    return false;
}
// MARK: - UNUserNotificationCenterDelegate Method
override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print(notification)
    completionHandler([.alert])
}


override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print(response)
    completionHandler()
}

}

Flutter Code

_initFirebaseMessaging() {
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) {
        print('AppPushs onMessage : $message');
        _showNotification(message);
        return;
      },
      onBackgroundMessage: Platform.isIOS ? myBackgroundMessageHandler : myBackgroundMessageHandler,
      onResume: (Map<String, dynamic> message) {
        print('AppPushs onResume : $message');
        if (Platform.isIOS) {
          _showNotification(message);
        }
        return;
      },
      onLaunch: (Map<String, dynamic> message) {
        print('AppPushs onLaunch : $message');
        return;
      },
    );

    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
    _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
  }

How can I solve this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM