问题描述
如何在iOS 10中获得通知?在以前的版本中,我可以在 func应用程序(应用程序:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject])中接收通知
.
How can I get notification in iOS 10? In previous version, I can receive notification in func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
.
在iOS 10中,Apple引入了 UNNotificationSettings
.我可以使用 func applicationReceivedRemoteMessage(remoteMessage:FIRMessagingRemoteMessage)
获取FCM.
In iOS 10, Apple introduces UNNotificationSettings
. I can get FCM with func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage)
.
但是,我无法在后台手机上收到通知.
However, I cannot get notification showing up on my phone in the background.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
authOptions,
completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
return true
}
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate, FIRMessagingDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
}
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) {
print("%@", remoteMessage.appData)
}
}
推荐答案
您必须移动此方法
application.registerForRemoteNotifications()
application.registerForRemoteNotifications()
上面的return语句.
to above return statement.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {
FIRApp.configure()
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
authOptions,
completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
NotificationCenter.default.addObserver(self,
selector: #selector(self.tokenRefreshNotification),
name: .firInstanceIDTokenRefresh,
object: nil)
return true
}
这篇关于推送通知未在iOS 10中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!