您的位置:首页 > 移动开发 > IOS开发

IOS8注册推送消息

2015-03-20 10:52 309 查看
IOS8在UIApplication文件中有说明:

- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types
NS_DEPRECATED_IOS(3_0,
8_0,
"Please use registerForRemoteNotifications and registerUserNotificationSettings: instead");

// Returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity.

- (UIRemoteNotificationType)enabledRemoteNotificationTypes
NS_DEPRECATED_IOS(3_0,
8_0,
"Please use -[UIApplication isRegisteredForRemoteNotifications], or -[UIApplication currentUserNotificationSettings] to retrieve user-enabled remote notification and user notification settings");

在IOS8下,采用了新的方法来注册推送和获取推送开启状态

注册推送的代码:
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=
8.0) {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert
| UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
    }

判断推送是否打开的代码:

+(BOOL)enabledRemoteNotification {

    UIRemoteNotificationType types;

    if ([[[UIDevice
currentDevice] systemVersion]
floatValue] >= 8.0) {

        types = [[UIApplication
sharedApplication] currentUserNotificationSettings].types;
    }
   
else {

        types = [[UIApplication
sharedApplication] enabledRemoteNotificationTypes];
    }

    return (types &
UIRemoteNotificationTypeAlert);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: