I'm building an iOS app with FCM push notifications. I followed the Google Firebase docs and other tutorials, but my AppDelegate never execute the function didReceiveRemoteNotification, so when I send notification from my Firebase Console it does nothing.
我正在开发一个带有FCM推送通知的iOS应用。我遵循了谷歌Firebase文档和其他教程,但是我的AppDelegate从不执行didReceiveRemoteNotification函数,所以当我从我的Firebase控制台发送通知时,它什么都不做。
I have this, for now:
我现在有这个:
AppDelegate.swift
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)
}
- I also installed Firebase on my CocoaPods file.
- 我还在我的CocoaPods文件上安装了Firebase。
- I registered the App in the Firebase Console with the same Build ID.
- 我在Firebase控制台使用相同的构建ID注册了这个应用程序。
- I have "Required Background Modes -> App downloads content in response to push notifications" in my info.plist file
- 我的信息中有“必要的后台模式——>应用程序下载内容以响应推送通知”。plist文件
- I've created and uploaded my APNS Certificate to my settings in Firebase Console.
- 我已经创建并上传了我的APNS证书到我的设置在Firebase控制台。
And when I start the App, it builds succesfully, and prints my this on console:
当我启动这个应用时,它成功地构建了我的这个,并在控制台打印出来:
[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.
I'm newbie on Swift, and I don't know if I forgot to add some code in AppDelegate or something.
我是Swift的新手,我不知道我是否忘记在AppDelegate中添加了一些代码。
Help please! I spent too much hour on this :(
请帮助!我在这上面花了太多的时间
2 个解决方案
#1
1
Before registering for push notifications you need to ask user's to grant you necessary privileges. Try this code in your didFinishLaunchingWithOptions method:
在注册推送通知之前,您需要请求用户授予您必要的权限。在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()
}
Also it would be useful to check the reason of app start. Add the following code in this method:
检查app start的原因也是很有用的。在此方法中添加以下代码:
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
self.application(application, didReceiveRemoteNotification: remoteNotification as! [AnyHashable : Any])
}
Additionally you may add didFailToRegisterForRemoteNotificationsWithError method to understand the reason of possible error:
此外,您可以添加didFailToRegisterForRemoteNotificationsWithError方法来了解可能出现错误的原因:
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print ("Eror: \(error.localizedDescription)")
}
#2
0
You need to register notification in appdelegate's didFinishLaunchingWithOptions
.
您需要在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
}
#1
1
Before registering for push notifications you need to ask user's to grant you necessary privileges. Try this code in your didFinishLaunchingWithOptions method:
在注册推送通知之前,您需要请求用户授予您必要的权限。在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()
}
Also it would be useful to check the reason of app start. Add the following code in this method:
检查app start的原因也是很有用的。在此方法中添加以下代码:
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
self.application(application, didReceiveRemoteNotification: remoteNotification as! [AnyHashable : Any])
}
Additionally you may add didFailToRegisterForRemoteNotificationsWithError method to understand the reason of possible error:
此外,您可以添加didFailToRegisterForRemoteNotificationsWithError方法来了解可能出现错误的原因:
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print ("Eror: \(error.localizedDescription)")
}
#2
0
You need to register notification in appdelegate's didFinishLaunchingWithOptions
.
您需要在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
}