栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

FCM推送通知在iOS 11中不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

FCM推送通知在iOS 11中不起作用

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Use Firebase library to configure APIs
FirebaseApp.configure()
self.registerForPushNotifications(application: application)
Messaging.messaging().delegate = self

    if let token = InstanceID.instanceID().token() {        NSLog("FCM TOKEN : (token)")        DataModel.sharedInstance.onSetUserFCMStringToken(FCM: token)        self.connectToFcm()    }    if launchOptions != nil {        //opened from a push notification when the app is closed        _ = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] ?? [AnyHashable: Any]()    }    else {        //opened app without a push notification.    }    return true}

@available(iOS 10,*)

extension AppDelegate: UNUserNotificationCenterDelegate {// iOS10+, called when presenting notification in foregroundfunc userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {    let userInfo = notification.request.content.userInfo    NSLog("[UserNotificationCenter] willPresentNotification: (userInfo)")    //TODO: Handle foreground notification    completionHandler([.alert])}// iOS10+, called when received response (default open, dismiss or custom action) for a notificationfunc userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {    let userInfo = response.notification.request.content.userInfo    NSLog("[UserNotificationCenter] didReceiveResponse: (userInfo)")    //TODO: Handle background notification    completionHandler()}}extension AppDelegate : MessagingDelegate {//MARK: FCM Token Refreshedfunc messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {    NSLog("[RemoteNotification] didRefreshRegistrationToken: (fcmToken)")}// Receive data message on iOS 10 devices while app is in the foreground.func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {    NSLog("remoteMessage: (remoteMessage.appData)")}}//Register for push notification.func registerForPushNotifications(application: UIApplication) {    if #available(iOS 10.0, *) {        let center  = UNUserNotificationCenter.current()        center.delegate = self        center.requestAuthorization(options: [.alert,.sound]) { (granted, error) in if error == nil{     DispatchQueue.main.async(execute: {         application.registerForRemoteNotifications()     }) }        }    }    else {        let settings = UIUserNotificationSettings(types: [.alert,.sound], categories: nil)        application.registerUserNotificationSettings(settings)        application.registerForRemoteNotifications()    }    // Add observer for InstanceID token refresh callback.    NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)}@objc func tokenRefreshNotification(_ notification: Notification) {    print(#function)    if let refreshedToken = InstanceID.instanceID().token() {        NSLog("Notification: refresh token from FCM -> (refreshedToken)")    }    // Connect to FCM since connection may have failed when attempted before having a token.    connectToFcm()}func connectToFcm() {    // Won't connect since there is no token    guard InstanceID.instanceID().token() != nil else {        NSLog("FCM: Token does not exist.")        return    }    Messaging.messaging().shouldEstablishDirectChannel = true}func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {    NSLog("Notification: Unable to register for remote notifications: (error.localizedDescription)")}// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to the InstanceID token.func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {    Messaging.messaging().apnsToken = deviceToken}// iOS9, called when presenting notification in foregroundfunc application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {    NSLog("didReceiveRemoteNotification for iOS9: (userInfo)")}func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636404.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号