如何检查"询问推送通知"已单击权限警报

时间:2015-01-04 16:58:10

标签: ios push-notification

如何检查用户是否以及何时对此视图执行操作?

我想这样做:

if notificationviewclosed{
    dosomething
}

然而,我无法找到一个合适的方法来检查这个。这是我的代码:

func SetupPushNotifications(){

    // Register for Push Notitications
    var userNotificationTypes:UIUserNotificationType = (UIUserNotificationType.Alert |
        UIUserNotificationType.Badge |
        UIUserNotificationType.Sound)

    if UIApplication.sharedApplication().respondsToSelector("isRegisteredForRemoteNotifications"){ //this should be done with getobjc method
        // iOS 8 Notifications
        var settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        UIApplication.sharedApplication().registerForRemoteNotifications()
    }else{
        // iOS < 8 Notifications
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
    }
}

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以通过在AppDelegate中实现以下两种方法来处理用户对提示的响应。

适用于iOS8:

     func application(_ application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)

For&lt; iOS8上:

     func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)

在此方法中,您可以通过调用以下内容并检查值

来检查用户是否已授予权限
application.enabledRemoteNotificationTypes()

有关详细信息,请参阅此处: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didRegisterUserNotificationSettings