简体   繁体   English

注册推送通知

[英]Registering for Push Notification

I have disabled Push notification from my device settings application (Inside my app icon under settings) and when I call following piece of code none of my delegate call back gets called.我已从我的设备设置应用程序中禁用推送通知(在设置下的我的应用程序图标内),当我调用以下代码时,我的委托回调都不会被调用。

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

application:didRegisterForRemoteNotificationsWithDeviceToken:
application:didFailToRegisterForRemoteNotificationsWithError:

Is there any way to know before registering for Push what all notification types have been switched on?在注册 Push 之前,有什么方法可以知道所有通知类型已打开? In my application, I am proceeding further once I receive the device token in didRegisterForRemoteNotificationsWithDeviceToken call back.在我的应用程序中,一旦我在 didRegisterForRemoteNotificationsWithDeviceToken 回调中收到设备令牌,我将继续进行。 Now, if user do not select any one of them I cannot proceed further so wanted to give an alternate path also.现在,如果用户没有 select 中的任何一个,我将无法继续进行,所以也想提供一条替代路径。

You can use您可以使用

UIRemoteNotificationType notificationTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

And then check the returned bit mask for what is and isn't enabled然后检查返回的位掩码是否启用和未启用

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     
}

You can read more in the docs here您可以在此处的文档中阅读更多信息

I realize this is getting fairly old, but there is a much better way to check which bits are set in a bitmask.我意识到这已经相当老了,但是有一种更好的方法来检查位掩码中设置了哪些位。 First of all, if you only want to check if at least one bit is set, check if the whole bitmask is not zero.首先,如果您只想检查是否设置了至少一个位,请检查整个位掩码是否不为零。

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

And if you want to check for specific bits being set, logically AND whichever bit you want to check with the bitmask.如果您想检查是否设置了特定位,请在逻辑上与您要使用位掩码检查的任何位。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM