2017-07-27 43 views
0

我需要检测用户何时允许或不允许推送通知。如何知道用户在推送通知中点击“不允许”

当用户点击允许或不允许第一个推送通知警报中的按钮时,我应该在服务器中调用推送API。 (让 - > pushYn = Y,不允许 - > pushYn = N) 和用户在iPhone的设置开启,关闭 - 通知

所以,我叫 “didRegisterForRemoteNotificationsWithDeviceToken” 的API 这样的代码

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     // Convert token to string 
     var token = "" 
     for i in 0..<deviceToken.count { 
      token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]]) 
     } 
     print(token) 
     pushTokenSetHttpRequest() 
    } 

但窃听“不允许”的用户,它不叫。

如何知道用户在推送通知提醒或开启iPhone的设置 - 通知时点击“不允许”?

注册通知

if #available(iOS 10, *) { 
      UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in } 
      application.registerForRemoteNotifications() 
     } 

     else { 
      UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 
      UIApplication.shared.registerForRemoteNotifications() 
     } 

谢谢

+0

您必须认识到,注册令牌时,您不需要获得用户的许可。静默通知未经许可即可使用。 **如果**想向用户显示某些内容(例如徽章/警报/声音),*然后*您需要获得他们的许可 – Honey

回答

1

不幸的是,我们没有准确的方法来检查用户是否拒绝。

但是您可以随时通过此方法检查是否获得许可。 UIApplication.shared.isRegisteredForRemoteNotifications这将只是告诉你,如果该应用程序实际上已经与苹果的推送服务器注册,并已经获得了设备令牌:

返回值是

如果应用程序注册为远程通知和 接受了它的设备令牌或NO,如果注册尚未发生,则 失败或已被用户拒绝。

+0

而不是回答重复问题,请将它们标记为重复项。 – JAL

+0

当然@JAL。谢谢 –

0

如果是iOS的水平弹出那么我相信它不可能找出如果用户已经聘请不允许在推送通知。但是,你可以随时检查您的推送通知是否启用或不使用下面的方法,并相应地导航到设置页面: -

func isPushEnabledAtOSLevel() -> Bool { 
guard let settings = UIApplication.shared.currentUserNotificationSettings?.types else { return false } 
return settings.rawValue != 0 
} 
0

取决于iOS版本:

// nil if not authorized. If I recall correctly, nullability was changed from iOS 8 to 9 correctly 
UIApplication.shared.currentUserNotificationSettings() 
// iOS 10+ (If you are using UNUserNotificationCenter). Docs link below 
UNUserNotificationCenter.current().getNotificationSettings(completionHandler:) 
UNUserNotificationCenter.current()authorizationStatus 



文档: https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649524-getnotificationsettings

相关问题