在 IOS 中未收到 FCM 推送通知。而应用程序在后台或终止。我使用 FirebaseMessaging 6.0.9 Pub

Posted

技术标签:

【中文标题】在 IOS 中未收到 FCM 推送通知。而应用程序在后台或终止。我使用 FirebaseMessaging 6.0.9 Pub【英文标题】:Not Receive FCM Push notification in IOS.While app in background or terminate.I m use FirebaseMessaging 6.0.9 Pub 【发布时间】:2021-01-22 08:06:20 【问题描述】:

我只在前台收到 Fcm 推送通知。应用在后台并终止时不会收到通知。

Swift 代码

AppDelegate.swift

导入 UIKit 导入颤振 导入谷歌地图 导入 Firebase 导入 Firebase 消息传递 导入 FirebaseInstanceID

@UIApplicationMain @objc 类 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()

颤动代码

_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");
    );
  

我该如何解决这个问题?

【问题讨论】:

问题:- github.com/FirebaseExtended/flutterfire/issues/2854 【参考方案1】:

问题。 https://github.com/FirebaseExtended/flutterfire/issues/2854#issuecomment-704922039

好消息,FCM 返工即将推出https://twitter.com/mikediarmid/status/1319298281290203139?s=20

【讨论】:

以上是关于在 IOS 中未收到 FCM 推送通知。而应用程序在后台或终止。我使用 FirebaseMessaging 6.0.9 Pub的主要内容,如果未能解决你的问题,请参考以下文章

iOS FCM 没有收到推送通知

iOS 12 中未提供 FCM 通知

通过 FCM 发送时未收到推送通知,但在 IOS 上通过 APN 发送时收到

在 iOS 中未收到推送通知消息

iOS 模拟器或物理设备未收到 FCM 数据消息

如果应用程序终止,则未收到 iOS FCM 推送通知