2017-07-27 42 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

相關問題