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];  
  21. } 

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

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

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
30分钟搞定iOS自定义相机 最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...