不接收从FCM到iOS的推送通知Swift 3

问题描述:

我正在构建一个带有FCM推送通知的iOS应用程序。我遵循Google Firebase文档和其他教程,但是我的AppDelegate从不执行函数didReceiveRemoteNotification,因此当我从Firebase控制台发送通知时,它什么也不做。不接收从FCM到iOS的推送通知Swift 3

我有这个,现在:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

     application.registerForRemoteNotifications()    
     FIRApp.configure() 
     connectToFcm() 

     return true 
    } 

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() 
    print("<-TOKEN->") 
    print(token) 
} 

func connectToFcm() { 
    FIRMessaging.messaging().connect { (error) in 
     if (error != nil) { 
      print("Unable to connect with FCM. \(error)") 
     } else { 
      print("Connected to FCM.") 
     } 
    } 
} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print("%@", userInfo) 
} 
  • 我也对我的CocoaPods文件安装火力地堡。
  • 我在具有相同Build ID的Firebase控制台中注册了应用程序。
  • 我有“所需的背景模式 - >响应 应用软件下载的内容推送通知”中
  • 我创建并上传我的APNS证书到我的火力地堡控制台设置我的Info.plist文件。

当我启动应用程序,它成功地建立,并打印我的这个控制台上:

[Firebase/Analytics][I-ACS023012] Firebase Analytics enabled 
2017-05-29 12:10:56.101 incidenciasSifu[1811] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled 
Optional(Error Domain=com.google.fcm Code=2001 "FIRMessaging is already connected" UserInfo={NSLocalizedFailureReason=FIRMessaging is already connected}) 
<-TOKEN-> 
Optional("03545fbbb986e2ffdfa50ac9e3eb6fe07e6fe4694cdfd000d673a0bf5ea53f6a") 
Connected to FCM. 

我对雨燕的新手,我不知道如果我忘了补充一些AppDelegate中的代码或其他东西。

请帮忙!我花了太多的时间在这个:(

推送通知注册之前,你需要问的用户授予您必要的权限 在didFinishLaunchingWithOptions方法试试这个代码:

if #available(iOS 10.0, *) { 
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in 
     if granted { 
      application.registerForRemoteNotifications() 
     } 
    } 
} else { 
    let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound] 
    let settings = UIUserNotificationSettings(types: notificationTypes, categories: nil) 
    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 
} 

也将是有益的检查应用程序启动的原因,添加以下代码在这个方法中:

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] { 
    self.application(application, didReceiveRemoteNotification: remoteNotification as! [AnyHashable : Any]) 
} 

此外您可以添加didFailToRegisterForRemoteNotificationsWithError方法,了解可能出现的错误的原因:

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    print ("Eror: \(error.localizedDescription)") 
} 
+0

好吧,我这样做,除了添加remoteNotification,我必须添加它的功能? – jbono

+0

前两部分应该替代您在didFinishLaunching中调用application.registerForRemoteNotifications() – Alexey

您需要在appdelegate的didFinishLaunchingWithOptions中注册通知。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 


     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
      // actions based on whether notifications were authorized or not 
     } 
     application.registerForRemoteNotifications() 
     return true 
    } 
+0

我已经有了application.registerForRemoteNotifications(),但没有center.requestAuthorization,是否有必要? – jbono

+0

是的,它是neccessory – KKRocks