我正在使用FCM推送通知构建iOS应用。我关注了Google Firebase文档和其他教程,但我的AppDelegate从未执行函数didReceiveRemoteNotification,因此当我从Firebase控制台发送通知时,它什么也没做。
我现在有这个:
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)
}
当我启动应用程序时,它会成功构建,并在控制台上打印出来:
[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.
我是Swift上的新手,我不知道是否忘记在AppDelegate中添加一些代码。
请帮忙!我在这上花了太多时间:(
答案 0 :(得分:1)
在注册推送通知之前,您需要让用户授予您必要的权限。 在didFinishLaunchingWithOptions方法中尝试此代码:
//The search code starts from here
while(first <= last && key >= first && key <= last) {
pos = (int)((double)first + (double)(last - first) * (double)(key - arr[first]) / (double)(arr[last] - arr[first]));
if(key < arr[pos]) {
last = pos - 1;
}
else if(key > arr[pos]) {
first = pos + 1;
}
else {
cout << "Found the value at index:\t" << pos << endl;
break;
}
}
检查应用启动的原因也很有用。在此方法中添加以下代码:
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()
}
此外,您可以添加didFailToRegisterForRemoteNotificationsWithError方法以了解可能出错的原因:
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] {
self.application(application, didReceiveRemoteNotification: remoteNotification as! [AnyHashable : Any])
}
答案 1 :(得分:0)
您需要在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
}