Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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

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
Ios Swift中的推送通知和令牌设备_Ios_Swift_Apple Push Notifications_Devicetoken - Fatal编程技术网

Ios Swift中的推送通知和令牌设备

Ios Swift中的推送通知和令牌设备,ios,swift,apple-push-notifications,devicetoken,Ios,Swift,Apple Push Notifications,Devicetoken,我正在开发一个应用程序,它需要知道设备令牌,以便在用户授予授权时向用户发送通知。 系统第一次请求授权通知时。如果用户说“允许”,系统将为我调用方法didRegisterForRemoteNotificationsWithDeviceToken,并在该方法的主体中写入UserDefaults设备令牌。 这就是流程: 1) 系统请求许可 2) 在didFinishLaunchingWithOptions(在AppDelegate内部)中,我在管理通知框架的类中调用此方法: func regis

我正在开发一个应用程序,它需要知道设备令牌,以便在用户授予授权时向用户发送通知。 系统第一次请求授权通知时。如果用户说“允许”,系统将为我调用方法didRegisterForRemoteNotificationsWithDeviceToken,并在该方法的主体中写入UserDefaults设备令牌。 这就是流程: 1) 系统请求许可 2) 在didFinishLaunchingWithOptions(在AppDelegate内部)中,我在管理通知框架的类中调用此方法:

   func registerForPushNotifications() {
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("NFM permission granted: \(granted)")
            // 1. Check if permission granted
            guard granted else { return }
            // 2. Attempt registration for remote notifications on the main thread
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
3) 系统为我调用了didRegisterForRemoteNotificationsWithDeviceToken(在AppDelegate中)方法,一切正常。 在方法主体中,我编写了令牌UserDefaults设置。 第二种情况发生在用户第一次拒绝许可时。 在第二种情况下,在我的应用程序的另一个ViewController中,我检查用户是否已使用设备设置授予推送通知的权限(因此退出应用程序)。在我的应用程序的特定部分中,我使用此方法验证用户是否已授予授权(始终在管理通知框架的类中)

在这种特定情况下,没有调用didRegisterForRemoteNotificationsWithDeviceToken方法,因此无法存储令牌。向用户询问授权的警报仅在用户首次使用打开应用时显示。 用户下次打开应用程序时,系统不再显示警报。 我怎样才能解决这个问题


谢谢。

我用以下方法解决这个问题: 在方法checkPremissionStatus中,我向我的方法registerForPushNotifications添加了一个a调用,通过这种方式,系统调用方法DidRegisterForRemotionTificationsWithDeviceToken,以便我可以在我的应用程序的业务逻辑中使用设备令牌。 这是在管理通知框架的类中修改的代码

    func checkPremissionStatus() -> Bool{
    var isAuth = false
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        print("NFM checkPremissionStatus -> notification status")
        switch settings.authorizationStatus {
        case .authorized:
            print("NFM checkPremissionStatus -> authorized status")
            self.autorizzato = true
            isAuth = true
        //TODO check pending notification...
        case .denied:
            print("NFM checkPremissionStatus -> denied status")
            self.autorizzato = false
            isAuth = false
        case .notDetermined:
            print("NFM notDetermined never see this print")
        }
    }
    return isAuth
}
    func checkPremissionStatus() -> Bool{
    var isAuth = false
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        print("NFM checkPremissionStatus -> notification status")
        switch settings.authorizationStatus {
        case .authorized:
            print("NFM checkPremissionStatus -> authorized status")
            self.autorizzato = true
            isAuth = true
            print("NFM checkPremissionStatus -> call registerForPushNotification")
            self.registerForPushNotifications()
        //TODO check pending notification...
        case .denied:
            print("NFM checkPremissionStatus -> denied status")
            self.autorizzato = false
            isAuth = false
        case .notDetermined:
            print("NFM notDetermined never see this print")
        }
    }
    return isAuth
}

因此,当我检查用户权限时,我发现用户已启用(从设备设置)推送通知案例。授权系统调用正确的方法,我可以将设备令牌存储在用户首选项中。

每次启动应用程序时,您只需注册推送通知即可。您不需要用户权限来注册推送通知,但是没有权限的通知将不会显示。您可以在未经许可的情况下接收无声推送。