Cloud messaging handing terminate app

前端 未结 4 821
生来不讨喜
生来不讨喜 2020-12-24 13:49

I have an application that stores the user\'s session in NSUserDefaults. When the application is forced to close, in the initial verify whether the data controller user sess

4条回答
  •  爱一瞬间的悲伤
    2020-12-24 14:23

    Do not forgot in AppDelegate:

    import Firebase
    import FirebaseInstanceID
    import FirebaseMessaging
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        registerForPushNotifications(application)
        FIRApp.configure()
    
        // Add observer for InstanceID token refresh callback.
        NSNotificationCenter
         .defaultCenter()
         .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
                                                         name: kFIRInstanceIDTokenRefreshNotification, object: nil)
    
        // Override point for customization after application launch.
        return true
      }
    
    func registerForPushNotifications(application: UIApplication) {
          let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
          application.registerUserNotificationSettings(settings)
          application.registerForRemoteNotifications()
      }
    
    
      func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                       fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        print("===== didReceiveRemoteNotification ===== %@", userInfo)
      }
    
    
     func tokenRefreshNotificaiton(notification: NSNotification) {
        let refreshedToken = FIRInstanceID.instanceID().token()!
        print("InstanceID token: \(refreshedToken)")
    
        // Connect to FCM since connection may have failed when attempted before having a token.
         connectToFcm()
      }
    
      func connectToFcm() {
        FIRMessaging.messaging().connectWithCompletion { (error) in
          if (error != nil) {
            print("Unable to connect with FCM. \(error)")
          } else {
            print("Connected to FCM.")
          }
        }
      }
    

    Please make sure also to do in Info.plist : FirebaseAppDelegateProxyEnabled = NO

    I don't know for now but I got the print(...) in didReceiveRemoteNotification but don't get the popup. I send the message from Firebase -> Console -> Notification -> Single device and copy here the token which I got from Xcode Console -> func tokenRefreshNotificaiton

提交回复
热议问题