免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 61081 | 回复: 3
打印 上一主题 下一主题

[iOS] ios如何实现本地推送,兼容ios8 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-01 10:36 |只看该作者 |倒序浏览
如果要兼容IOS8在IOS中实现本地推送,关键是要注意:ios8在实现本地推送时需要通过如下语句进行注册。
  1. [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
复制代码
至于IOS8之前版本的做法就不多说了,直接上代码。新建oc类文件(NotificationHelper),在NotificationHelper.h中声明相关方法如下:
  1. #import <UIKit/UIKit.h>

  2. @interface NotificationHelper:NSObject <UIApplicationDelegate>
  3. {
  4. }
  5. -(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
  6. -(void)removeLocalNotificationByKey:(NSString*)key;
  7. -(void)removeLocalAllNotification;
  8. -(void) registerLocalNotification:(UIApplication*)application;
  9. +(NotificationHelper*) shareInstance;
  10. @end
复制代码
在NotificationHelper.m文件中实现方法如下:
  1. #import "NotificationHelper.h"

  2. @implementation NotificationHelper
  3. static NotificationHelper* instance;
  4. //实现单例
  5. +(NotificationHelper*) shareInstance
  6. {
  7.     static dispatch_once_t onceToken ;
  8.     dispatch_once(&onceToken, ^{
  9.         instance = [[super allocWithZone:NULL] init] ;
  10.     });
  11.     return instance ;
  12. }
  13. //推送处理[注册消息通知]
  14. -(void) registerLocalNotification:(UIApplication*)application
  15. {
  16.     application.applicationIconBadgeNumber = 0;//清除应用图标上的数字
  17. //关键:加上版本的控制
  18. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
  19.     // The following line must only run under iOS 8. This runtime check prevents
  20.     // it from running if it doesn't exist (such as running under iOS 7 or earlier).
  21.     UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
  22.     UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
  23.     if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
  24.     {
  25.         [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
  26.     }
  27. #endif
  28. }
  29. -(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
  30. {
  31.     NSLog(@"addNotifiction");
  32.     UILocalNotification *localNotification = [[UILocalNotification alloc] init];
  33.     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  34.     [formatter setDateFormat:@"HH:mm:ss"];
  35.     NSDate *now = [formatter dateFromString:firedate];//触发通知的时间
  36.   //如果firedate传入的是XX:XX:XX格式在表示在固定的时间点发送通知,如果传入的是XX格式表示从现在开始XX秒后发送通知
  37.     if(now == nil)
  38.     {
  39.         NSTimeInterval secs = [firedate doubleValue];
  40.         now = [NSDate dateWithTimeIntervalSinceNow:secs];
  41.     }
  42.     localNotification.fireDate = now;
  43.     //设置 时区
  44.     localNotification.timeZone = [NSTimeZone defaultTimeZone];
  45.     // 触发后,弹出警告框中显示的内容
  46.     localNotification.alertBody = message;
  47.     localNotification.alertAction = NSLocalizedString(@"View Details", nil);
  48.     // 触发时的声音(这里选择的系统默认声音)
  49.     localNotification.soundName = UILocalNotificationDefaultSoundName;
  50.     // 触发频率(repeatInterval是一个枚举值,可以选择每分、每小时、每天、每年等)
  51.     localNotification.repeatInterval = kCFCalendarUnitDay;//测试用暂时写死为每隔一天 0:不重复
  52.     // 需要在App icon上显示的未读通知数(设置为1时,多个通知未读,系统会自动加1,如果不需要显示未读数,这里可以设置0)
  53.     localNotification.applicationIconBadgeNumber = 1;
  54.     // 设置通知的id,可用于通知移除,也可以传递其他值,当通知触发时可以获取
  55.     localNotification.userInfo = @{@"id" : key};
  56.     // 注册本地通知
  57.     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
  58.     [localNotification release];
  59. }
  60. /**
  61. removeLocalNotificationByKey
  62. */
  63. - (void)removeLocalNotificationByKey:(NSString*)key {
  64.     // 取出全部本地通知
  65.     NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
  66.     // 设置要移除的通知id
  67.     NSString *notificationId = key;
  68.     // 遍历进行移除
  69.     for (UILocalNotification *localNotification in notifications) {
  70.         // 将每个通知的id取出来进行对比
  71.         if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:notificationId]) {
  72.             // 移除某一个通知
  73.             [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
  74.         }
  75.     }
  76. }

  77. - (void)removeLocalAllNotification {
  78.     [[UIApplication sharedApplication] cancelAllLocalNotifications];
  79. }
  80. @end
复制代码
用法举例:

比如在应用启动的时候调在didFinishLaunchingWithOptions方法中调用:

[[NotificationHelper shareInstance] registerLocalNotification:application];

进行注册和版本控制,在需要发送通知的时候调用:

[[NotificationHelper shareInstance] addNotifiction:"18:30:30" keyA:"key" messageA:"可以领取体力了!" ]

完毕。由于公司的手游项目需要使用到本地推送,而我们的项目是用quick cocos2d-x引擎,前端使用LUA编写脚本和界面。这样就面临一个问题:如何编写友好的接口让lua能够调用oc来实现推送,这样的话所有的逻辑都在lua中实现。

下次有空再说。

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-06-13 22:20:00
2 [报告]
发表于 2015-06-12 18:26 |只看该作者
哈哈哈 ,不错。

论坛徽章:
0
3 [报告]
发表于 2019-05-13 10:59 |只看该作者
现在有很多第三方sdk都做推送,我这边有使用过mobpush这个第三方的,他们这边的sdk使用起来还是不错的,集成起来步骤比较少,服务也挺不错,值得推荐下,有兴趣可以去了解下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP