iOS8检查remotenotificationtype的权限

woh*_*ras 4 deprecated push-notification apple-push-notifications ios ios8

我可以检查用户是否在iOS8之前授予了通知(警告)权限:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
{
    //user granted
}
Run Code Online (Sandbox Code Playgroud)

它不适用于iOS8,它说:

iOS (3.0 and later) Deprecated:Register for user notification settings using the  registerUserNotificationSettings: method instead.
Run Code Online (Sandbox Code Playgroud)

控制台说:

enabledRemoteNotificationTypes is not supported in iOS 8.0 and later.
Run Code Online (Sandbox Code Playgroud)

那么如何在iOS 8上查看呢?

woh*_*ras 9

好的,我这样解决了我的问题:

BOOL isgranted = false;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        isgranted =  [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    }
#else
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert)
    {
        isgranted = true;
    }
#endif
Run Code Online (Sandbox Code Playgroud)

  • 嗨,您的 IOS8 解决方案并不能完全告诉您“允许通知”是打开还是关闭。如果允许通知设置关闭,“isRegisteredForRemoteNotifications”将返回 false,但如果声音通知被禁用且警报类型设置为“none”,也会返回 false。有什么方法可以具体确定是否启用了允许通知,完全由它自己决定? (2认同)