IOS学习笔记11-如何判断推送开关是否打开
2014-03-19 14:29
411 查看
最近做项目有一个需求,要在程序得系统设置里进行push的设置。在网上搜了几天资料没找着啥。今天忽然心血来潮跟踪系统注册push时得代码,居然发现有可行得解决方法,思路如下:
1、在iphone得framework里的UIApplication.h中有以下函数:
@interface UIApplication (UIRemoteNotifications)
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types __OSX_***AILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)unregisterForRemoteNotifications __OSX_***AILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // calls -registerForRemoteNotificationTypes with UIRemoteNotificationTypeNone
// returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity
- (UIRemoteNotificationType)enabledRemoteNotificationTypes __OSX_***AILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
@end
2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]获取到允许得push推送类型。然后再调用registerForRemoteNotificationTypes进行修改。若要关闭程序得push服务,可调用unregisterForRemoteNotifications.
3、补充:以上想法以实现。补充部分代码。settingsData为tableview的数据源数组
a、获取系push设置,用于显示给用户
//push设置
NSMutableArray * pushOptions = [[NSMutableArray alloc] init];
UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"声音", @"name",
@"0", @"status",
nil];
if (notificationType & UIRemoteNotificationTypeSound) {
[soundNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:soundNotice];
[soundNotice release];
NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"提醒", @"name",
@"0", @"status",
nil];
if (notificationType & UIRemoteNotificationTypeAlert) {
[alertNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:alertNotice];
[alertNotice release];
NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"标记", @"name",
@"0", @"status",
nil];
if (notificationType & UIRemoteNotificationTypeBadge) {
[badgeNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:badgeNotice];
[badgeNotice release];
NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:
@"通知设置", @"groupName",
pushOptions, @"data",
nil];
[self.settingsData addObject:pushConfig];
[pushOptions release];
[pushConfig release];
b、获取用户设置的数据放入pushdata,然后向系统提交设置
NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];
NSInteger length = [pushData count];
UIRemoteNotificationType myType = 0;
for (NSInteger i =0; i< length; i++) {
if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1) {
switch (i) {
case 0: myType = myType|UIRemoteNotificationTypeSound; break;
case 1: myType = myType|UIRemoteNotificationTypeAlert; break;
case 2: myType = myType|UIRemoteNotificationTypeBadge; break;
default: break;
}
}
}
if (myType != 0) {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];
}else {
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
}
1、在iphone得framework里的UIApplication.h中有以下函数:
@interface UIApplication (UIRemoteNotifications)
- (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types __OSX_***AILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)unregisterForRemoteNotifications __OSX_***AILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // calls -registerForRemoteNotificationTypes with UIRemoteNotificationTypeNone
// returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity
- (UIRemoteNotificationType)enabledRemoteNotificationTypes __OSX_***AILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
@end
2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]获取到允许得push推送类型。然后再调用registerForRemoteNotificationTypes进行修改。若要关闭程序得push服务,可调用unregisterForRemoteNotifications.
3、补充:以上想法以实现。补充部分代码。settingsData为tableview的数据源数组
a、获取系push设置,用于显示给用户
//push设置
NSMutableArray * pushOptions = [[NSMutableArray alloc] init];
UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"声音", @"name",
@"0", @"status",
nil];
if (notificationType & UIRemoteNotificationTypeSound) {
[soundNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:soundNotice];
[soundNotice release];
NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"提醒", @"name",
@"0", @"status",
nil];
if (notificationType & UIRemoteNotificationTypeAlert) {
[alertNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:alertNotice];
[alertNotice release];
NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"标记", @"name",
@"0", @"status",
nil];
if (notificationType & UIRemoteNotificationTypeBadge) {
[badgeNotice setValue:@"1" forKey:@"status"];
}
[pushOptions addObject:badgeNotice];
[badgeNotice release];
NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:
@"通知设置", @"groupName",
pushOptions, @"data",
nil];
[self.settingsData addObject:pushConfig];
[pushOptions release];
[pushConfig release];
b、获取用户设置的数据放入pushdata,然后向系统提交设置
NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];
NSInteger length = [pushData count];
UIRemoteNotificationType myType = 0;
for (NSInteger i =0; i< length; i++) {
if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1) {
switch (i) {
case 0: myType = myType|UIRemoteNotificationTypeSound; break;
case 1: myType = myType|UIRemoteNotificationTypeAlert; break;
case 2: myType = myType|UIRemoteNotificationTypeBadge; break;
default: break;
}
}
}
if (myType != 0) {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];
}else {
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
}
相关文章推荐
- iOS怎么判断用户是否打开了系统设置里面推送通知的开关?
- iOS技术点:判断用户是否开启推送开关/是否允许推送?
- iOS技术点:判断用户是否开启推送开关/是否允许推送?
- iOS判断用户是否打开APP通知开关
- iOS开发之判断用户是否打开APP通知开关,看我就够了。
- iOS学习笔记之textfield判断是否是全汉字、全数字、全字母!
- iOS应用内判断推送开发是否打开
- iOS笔记:判断相机是否被授权,应用是否能够打开相机
- iOS技术点:判断用户是否开启推送开关/是否允许推送?
- 【iOS知识学习】_如何判断手机是否为静音模式
- Python OpenCV学习笔记之:判断OpenCV是否已经打开优化功能
- 判断iOS的推送是否打开
- iOS中判断消息推送是否打开
- 【iOS知识学习】_如何判断手机是否为静音模式
- iOS开发之判断用户是否打开APP通知开关
- IOS中判断软件消息推送是否打开
- iOS开发之判断用户是否打开APP通知开关
- iOS学习笔记39-iOS开发,谓词(NSPredicate)的用法:(二)谓词和正则表达式配合使用,进行表单内容判断
- Activiti 学习笔记十:开始活动节点(判断流程是否结束及查询历史)
- iOS学习笔记—11 常见设计模式