iPhone开发基础学习 在程序里设置Push
创始人
2024-08-02 18:20:28
0

iPhone开发基础学习 在程序里设置Push是本文要介绍的内容,最近做项目有一个需求,要在程序得系统设置里进行push的设置。在网上搜了几天资料没找着啥。今天忽然心血来潮跟踪系统注册push时得代码,居然发现有可行得解决方法,思路如下:

1、在iphone得framework里的UIApplication.h中有以下函数:

  1. @interface UIApplication (UIRemoteNotifications)  
  2. - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  3. - (void)unregisterForRemoteNotifications __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);       
  4. // calls -registerForRemoteNotificationTypes with UIRemoteNotificationTypeNone  
  5.  
  6. // returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity  
  7. - (UIRemoteNotificationType)enabledRemoteNotificationTypes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  8. @end 

2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]获取到允许得push推送类型。然后再调用registerForRemoteNotificationTypes进行修改。若要关闭程序得push服务,可调用unregisterForRemoteNotifications.

3、补充:以上想法以实现。补充部分代码。settingsData为tableview的数据源数组

a、获取系push设置,用于显示给用户

  1. //push设置  
  2.  
  3. NSMutableArray * pushOptions = [[NSMutableArray alloc] init];  
  4.  
  5. UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];  
  6.  
  7.  
  8. NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  9.                                      @"声音", @"name",  
  10.                                      @"0",   @"status",  
  11.                                      nil];  
  12. if (notificationType & UIRemoteNotificationTypeSound) {  
  13.     [soundNotice setValue:@"1" forKey:@"status"];  
  14. }          
  15. [pushOptions addObject:soundNotice];  
  16. [soundNotice release];  
  17.  
  18.  
  19. NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  20.                                      @"提醒", @"name",  
  21.                                      @"0",   @"status",  
  22.                                      nil];  
  23. if (notificationType & UIRemoteNotificationTypeAlert) {  
  24.     [alertNotice setValue:@"1" forKey:@"status"];  
  25. }          
  26. [pushOptions addObject:alertNotice];  
  27. [alertNotice release];  
  28.  
  29.  
  30. NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
  31.                                      @"标记", @"name",  
  32.                                      @"0",   @"status",  
  33.                                      nil];  
  34. if (notificationType & UIRemoteNotificationTypeBadge) {  
  35.     [badgeNotice setValue:@"1" forKey:@"status"];  
  36. }          
  37. [pushOptions addObject:badgeNotice];  
  38. [badgeNotice release];  
  39.  
  40.  
  41. NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:  
  42.                               @"通知设置", @"groupName",  
  43.                               pushOptions,    @"data",  
  44.                               nil];  
  45. [self.settingsData addObject:pushConfig];  
  46. [pushOptions release];  
  47. [pushConfig release]; 

b、获取用户设置的数据放入pushdata,然后向系统提交设置

  1. NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];  
  2. NSInteger length = [pushData count];  
  3.  
  4. UIRemoteNotificationType myType = 0;  
  5.  
  6. for (NSInteger i =0; i< length; i++) {  
  7.     if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1) {  
  8.         switch (i) {  
  9.             case 0:        myTypemyType = myType|UIRemoteNotificationTypeSound;    break;  
  10.             case 1:        myTypemyType = myType|UIRemoteNotificationTypeAlert;    break;  
  11.             case 2:        myTypemyType = myType|UIRemoteNotificationTypeBadge;    break;  
  12.             default:    break;  
  13.         }  
  14.     }  
  15. }  
  16.  
  17. if (myType != 0) {  
  18.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];  
  19. }else {  
  20.     [[UIApplication sharedApplication] unregisterForRemoteNotifications];  

希望以上思路对有这方面需求得人有帮助。以上方案我暂未用于代码实现。若有问题。请留言共同商讨。

小结:iPhone开发基础学习 在程序里设置Push的内容介绍完了,希望本文对你有所帮助1更多相关内容请参考编辑推荐。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...