zhaozy78 发表于 2015-06-01 10:36

ios如何实现本地推送,兼容ios8

如果要兼容IOS8在IOS中实现本地推送,关键是要注意:ios8在实现本地推送时需要通过如下语句进行注册。[ registerUserNotificationSettings:mySettings];至于IOS8之前版本的做法就不多说了,直接上代码。新建oc类文件(NotificationHelper),在NotificationHelper.h中声明相关方法如下:#import <UIKit/UIKit.h>

@interface NotificationHelper:NSObject <UIApplicationDelegate>
{
}
-(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
-(void)removeLocalNotificationByKey:(NSString*)key;
-(void)removeLocalAllNotification;
-(void) registerLocalNotification:(UIApplication*)application;
+(NotificationHelper*) shareInstance;
@end在NotificationHelper.m文件中实现方法如下:#import "NotificationHelper.h"

@implementation NotificationHelper
static NotificationHelper* instance;
//实现单例
+(NotificationHelper*) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
      instance = [ init] ;
    });
    return instance ;
}
//推送处理[注册消息通知]
-(void) registerLocalNotification:(UIApplication*)application
{
    application.applicationIconBadgeNumber = 0;//清除应用图标上的数字
//关键:加上版本的控制
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    // The following line must only run under iOS 8. This runtime check prevents
    // it from running if it doesn't exist (such as running under iOS 7 or earlier).
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = ;
    if ()
    {
      [ registerUserNotificationSettings:mySettings];
    }
#endif
}
-(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
{
    NSLog(@"addNotifiction");
    UILocalNotification *localNotification = [ init];
    NSDateFormatter *formatter = [ init];
    ;
    NSDate *now = ;//触发通知的时间
  //如果firedate传入的是XX:XX:XX格式在表示在固定的时间点发送通知,如果传入的是XX格式表示从现在开始XX秒后发送通知
    if(now == nil)
    {
      NSTimeInterval secs = ;
      now = ;
    }
    localNotification.fireDate = now;
    //设置 时区
    localNotification.timeZone = ;
    // 触发后,弹出警告框中显示的内容
    localNotification.alertBody = message;
    localNotification.alertAction = NSLocalizedString(@"View Details", nil);
    // 触发时的声音(这里选择的系统默认声音)
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    // 触发频率(repeatInterval是一个枚举值,可以选择每分、每小时、每天、每年等)
    localNotification.repeatInterval = kCFCalendarUnitDay;//测试用暂时写死为每隔一天 0:不重复
    // 需要在App icon上显示的未读通知数(设置为1时,多个通知未读,系统会自动加1,如果不需要显示未读数,这里可以设置0)
    localNotification.applicationIconBadgeNumber = 1;
    // 设置通知的id,可用于通知移除,也可以传递其他值,当通知触发时可以获取
    localNotification.userInfo = @{@"id" : key};
    // 注册本地通知
    [ scheduleLocalNotification:localNotification];
    ;
}
/**
removeLocalNotificationByKey
*/
- (void)removeLocalNotificationByKey:(NSString*)key {
    // 取出全部本地通知
    NSArray *notifications = .scheduledLocalNotifications;
    // 设置要移除的通知id
    NSString *notificationId = key;
    // 遍历进行移除
    for (UILocalNotification *localNotification in notifications) {
      // 将每个通知的id取出来进行对比
      if ([ isEqualToString:notificationId]) {
            // 移除某一个通知
            [ cancelLocalNotification:localNotification];
      }
    }
}

- (void)removeLocalAllNotification {
    [ cancelAllLocalNotifications];
}
@end用法举例:

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

[ registerLocalNotification:application];

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

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

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

下次有空再说。

chen_sixYear 发表于 2015-06-12 18:26

哈哈哈 ,不错。{:yxh31:}

llli 发表于 2019-05-13 10:59

现在有很多第三方sdk都做推送,我这边有使用过mobpush这个第三方的,他们这边的sdk使用起来还是不错的,集成起来步骤比较少,服务也挺不错,值得推荐下,有兴趣可以去了解下
页: [1]
查看完整版本: ios如何实现本地推送,兼容ios8