Firebase推送通知不适用于iOS

时间:2020-02-06 13:32:25

标签: ios swift firebase firebase-cloud-messaging apple-push-notifications

我想使用Firebase Cloud Messaging实施推送通知

我已经按照说明设置了项目并上传了APN证书 并且我正在使用 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() registerForPushNotifications(app: application) return true } func registerForPushNotifications(app: UIApplication) { UNUserNotificationCenter.current().delegate = self Messaging.messaging().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (authorized, error) in if let error = error { print(error.localizedDescription) return } if authorized { print("authorized") DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } else { print("denied") } } app.registerForRemoteNotifications() } func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase registration token: \(fcmToken)") let dataDict:[String: String] = ["token": fcmToken] NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict) // TODO: If necessary send token to application server. // Note: This callback is fired at each app startup and whenever a new token is generated. } func application(_ application: UIApplication, didReceiveRemoteNotification notification: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("notofication arrivied") if Auth.auth().canHandleNotification(notification) { completionHandler(.noData) return } // This notification is not auth related, developer should handle it. } 向我的真实设备发送测试消息

我的配置在AppDelegate中如下

notofication arrivied

应该看到List<Minesweeper> minesweeperList = new ArrayList<>();,但没有设置尖点似乎这部分没有被原谅,因此消息没有发出

2 个答案:

答案 0 :(得分:0)

除非您启用了Swizzling,否则我不会在您的AppDelegate中看到此

func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
      Messaging.messaging().apnsToken = deviceToken
    }

此代码将您的APNs设备令牌映射到FCM令牌,这是必要的,因为APNs令牌是发送推送通知的唯一方法。

答案 1 :(得分:0)

func sendPushNotification(to token: String, title: String, body: String, userInfo: [String: Any]) {
    let payload: [String: Any] = ["title": title, "body": body, "sound": "sound.caf"]
    let paramString: [String: Any] = ["to": token, "notification": payload, "data": userInfo]
    
    let urlString = "https://fcm.googleapis.com/fcm/send"
    let url = NSURL(string: urlString)!
    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "POST"
    request.httpBody = try? JSONSerialization.data(withJSONObject:paramString, options: [.prettyPrinted])
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("key=\(Keys.gmsServerKey)", forHTTPHeaderField: "Authorization")

    let task =  URLSession.shared.dataTask(with: request as URLRequest)  { (data, response, error) in
        do {
            if let data = data {
                if let object  = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: Any] {
                    NSLog("Received data: \(object))")
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
        }
    }
    task.resume()
}
相关问题