注册推送通知

Abh*_*nav 5 iphone cocoa-touch objective-c push-notification ios

我已经从我的设备设置应用程序(设置中的我的应用程序图标内)禁用了推送通知,当我调用以下代码时,我的代理回调都没有被调用.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound];

application:didRegisterForRemoteNotificationsWithDeviceToken:
application:didFailToRegisterForRemoteNotificationsWithError:
Run Code Online (Sandbox Code Playgroud)

在注册Push之前有什么方法可以知道所有通知类型是否已经打开?在我的应用程序中,一旦我在didRegisterForRemoteNotificationsWithDeviceToken回调中收到设备令牌,我就会继续前进.现在,如果用户没有选择其中任何一个,我就无法继续前进,所以也希望给出一个替代路径.

Mic*_*gle 10

您可以使用

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
Run Code Online (Sandbox Code Playgroud)

然后检查返回的位掩码,了解是什么和不启用

if (notificationTypes == UIRemoteNotificationTypeNone) {
    // Do what ever you need to here when notifications are disabled
} else if (notificationTypes == UIRemoteNotificationTypeBadge) {
    // Badge only
} else if (notificationTypes == UIRemoteNotificationTypeAlert) {
    // Alert only
} else if (notificationTypes == UIRemoteNotificationTypeSound) {
    // Sound only
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)) {
    // Badge & Alert
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)) {
    // Badge & Sound        
} else if (notificationTypes == (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
    // Alert & Sound
} else if (notificationTypes == (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)) {
    // Badge, Alert & Sound     
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读更多文档


bdm*_*ntz 9

我意识到这已经相当老了,但有一种更好的方法来检查在位掩码中设置了哪些位.首先,如果您只想检查是否设置了至少一位,请检查整个位掩码是否为零.

if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] != 0) {
    //at least one bit is set
}
Run Code Online (Sandbox Code Playgroud)

如果你想检查设置的特定位,逻辑上和你要用位掩码检查的位.

UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (enabledTypes & UIRemoteNotificationTypeBadge) {
    //UIRemoteNotificationTypeBadge is set in the bitmask
}
Run Code Online (Sandbox Code Playgroud)