Cloud messaging handing terminate app

前端 未结 4 830
生来不讨喜
生来不讨喜 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:38

    Here is the solution,

    First Upload the necessary certificates in Firebase Console Then in your app enable Push Notifications and Background Modes -> Remote Notifications

    After that in App Delegate use the code below(I specify the tricky line) :

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        registerForPushNotifications(application)
        // Override point for customization after application launch.
        // Use Firebase library to configure APIs
        FIRApp.configure()
        return true
    }
    
    func registerForPushNotifications(application: UIApplication) {
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
    }
    
    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        if notificationSettings.types != .None {
            application.registerForRemoteNotifications()
        }
    }
    
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        let tokenChars = UnsafePointer(deviceToken.bytes)
        var tokenString = ""
    
        for i in 0..

提交回复
热议问题