Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我';我无法在iOS 10中处理收到的推送通知?_Ios_Swift_Firebase_Apple Push Notifications_Firebase Notifications - Fatal编程技术网

为什么我';我无法在iOS 10中处理收到的推送通知?

为什么我';我无法在iOS 10中处理收到的推送通知?,ios,swift,firebase,apple-push-notifications,firebase-notifications,Ios,Swift,Firebase,Apple Push Notifications,Firebase Notifications,我已经进口了 import UserNotifications 和关联代表: UNUserNotificationCenterDelegate 并将代理设置为自己: UNUserNotificationCenter.currentNotificationCenter().delegate = self FIRMessaging.messaging().remoteMessageDelegate = self 最后,我有以下功能: @available(iOS 10.0, *) func u

我已经进口了

import UserNotifications
和关联代表:

UNUserNotificationCenterDelegate
并将代理设置为自己:

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)
}

请尝试以下代码。你只需要保存数据

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")
       }
   }

并在要使用的位置使用用户默认值。

请使用以下代码进行尝试。你只需要保存数据

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")
       }
   }
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)
 }

并在要使用的地方使用用户默认值。

必须使用断点调试代码。如果此方法未调用DidReceiveMotonification。然后你必须检查后端。只有当我在应用程序中时,它才会调试。但是当我处于后台模式时,我收到通知,但无法处理@amanpretp请检查下面的答案。为什么要使用两种“DidReceiveEmotentification”方法?必须使用断点调试代码。如果此方法未调用DidReceiveMotonification。然后你必须检查后端。只有当我在应用程序中时,它才会调试。但是当我处于后台模式时,我收到通知,但无法处理@amanpretp请检查下面的答案。你为什么要使用两种“DidReceiveMemotentification”方法?看看我更新了我的答案。你可以在这里检查,当然你必须打电话给完成处理3R??检查我已经更新了我的答案。您可以在这里查看,当然您必须致电完成处理3R??
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)
 }