Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
如何在swift(FCM)中接收数据通知_Swift_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

如何在swift(FCM)中接收数据通知

如何在swift(FCM)中接收数据通知,swift,firebase,push-notification,firebase-cloud-messaging,Swift,Firebase,Push Notification,Firebase Cloud Messaging,我需要在swift中接收数据通知,而不是使用通知消息。仅使用数据消息 这是我写给邮递员的Json { "to": "e32uafd....", "data": { "urlImage": "https://upload.wikimedia.org/wikipedia/commons/8/80/image.jpg", "title": "title", "body": "****body****.", "mutable_c

我需要在swift中接收数据通知,而不是使用通知消息。仅使用数据消息

这是我写给邮递员的Json

{

"to": "e32uafd....",

    "data": {
        "urlImage": "https://upload.wikimedia.org/wikipedia/commons/8/80/image.jpg",
        "title": "title",
        "body": "****body****.",
        "mutable_content": true
    }
}
这是我的swift密码

@available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void)
    {
                switch UIApplication.shared.applicationState
                    {
                    case .active:
                        completionHandler( [.alert,.sound])
                        break
                    default:
                        completionHandler( [.alert,.sound])
                        break
                          }
                             }
        }   
  }

您可以使用
func应用程序(application:UIApplication,DidReceiveMemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult)->Void)获取通知数据
但在执行此操作之前,您必须使您的ViewController成为注册通知时或在
didFinishLaunchWithOptions(:)
方法中的
UNUserNotificationCenter
的代表

步骤1 使您的AppDelegate成为UNNotificationCenter的代理

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

     // Assumming you have imported NotificationCenter
     UNUserNotificationCenter.current().delegate = self
    //......

        return true
    }


步骤2 获取您的信息

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

     print(userInfo) // Your notification info
    // Note that you have to cast/parse it depending on which data you want to handle. 


    }


注册通知时,您必须将您的应用程序代理指定为
UNUserNotificationCenter
的代理。我编辑了我的答案只是为了澄清。你确定你的应用程序收到推送通知吗?不,我没有收到通知。但是如果我添加“通知”:{“标题”:“标题”,“正文”:“****正文****”,“可变内容”:true},我会收到通知,但这就是问题所在,我不需要使用通知消息。仅使用数据消息