我已导入
import UserNotifications
和连接的委托:
UNUserNotificationCenterDelegate
并将代理设置为self:
UNUserNotificationCenter.currentNotificationCenter().delegate = self
FIRMessaging.messaging().remoteMessageDelegate = self
最后我有这些功能:
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
print("User Info = ",notification.request.content.userInfo)
completionHandler([.Alert, .Badge, .Sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
//Handle the notification
print("User Info = ",response.notification.request.content.userInfo)
completionHandler()
}
但是当我在真实设备上以及在后台模式下进行测试时,我不会记录任何打印件。
有什么问题?
internal func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print(userInfo)
}
答案 0 :(得分:2)
请尝试使用以下代码。您只需要保存数据。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> ())
{
let info : NSDictionary! = userInfo as! NSDictionary
if info != nil
{
let aps = info["aps"] as? NSDictionary
UserDefaults.standard.set(aps, forKey: "aps")
UserDefaults.synchronize()
}
}
并使用您要使用的用户默认值。
答案 1 :(得分:0)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let info = userInfo as! [String : AnyObject]
let aps = info["aps"] as? NSDictionary
print(aps)
}