2016-09-14 81 views
1

有沒有辦法通過系統警報檢查應用程序是否獲得推送通知許可?有沒有辦法檢查推送通知設置是否被請求?

UIApplication.sharedApplication().currentUserNotificationSettings()?.types.rawValue

它返回0既當應用程序從未要求推送通知允許當用戶拒絕的權限。

我想在應用程序從不請求權限時顯示系統警報彈出窗口。如果用戶拒絕了權限,我還想讓用戶去設置權限設置。

例如:

let pushNotificationPermissionNeverAcquired: Bool = ??? 
if (pushNotificationPermissionNeverAcquired) { 
    // show system popup to acquire push notification permission 
    UIApplication.sharedApplication.registerForRemoteNotificationTypes([ * some types ]) 

} else if (UIApplication.sharedApplication().currentUserNotificationSettings()?.types.rawValue == 0) { 
    // user declined push notification permission, so let user go to setting and change the permission 
    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)) 

} else { 
    // push notification is allowed 

} 

我如何檢查pushNotificationPermissionNeverAcquired

回答

1

這就是我現在正在做的事情。

if NSUserDefaults.standardUserDefaults().stringForKey(NOTIFICATION_PERMISSIONS_ASKED) == nil 
    { 
     NSUserDefaults.standardUserDefaults().setObject("true", forKey: NOTIFICATION_PERMISSIONS_ASKED) 
     // Request permissions 
    } 
    else 
    { 
     // Show your own alert 
    } 

你可以保存一個布爾太多,如果你想與「boolForKey」訪問它,如果沒有密鑰發現返回false。

忘記了這一點。

if let enabledTypes = UIApplication.sharedApplication().currentUserNotificationSettings()?.types { 
    switch enabledTypes 
    { 
    case UIUserNotificationType.None: 
     // Denied permissions or never given 
    default: 
     // Accepted 
    } 
} 
+0

這是可能的想法,但我需要採用它到已經發布的應用程序。將有用戶允許/不允許推送通知,但沒有保存的值。 – Ryan

+0

如何請求權限,然後檢查您當前的視圖控制器是否呈現警報控制器? (系統不允許/允許)。我不確定它是否會成爲視圖層次結構的一部分。 – ohr

相關問題