您所在的位置:小祥子 » 编程 » IOS » 正文

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

时间:2015-05-23 编辑:老桂 来源:本站整理

如果要兼容IOS8在IOS中实现本地推送,关键是要注意:ios8在实现本地推送时需要通过如下语句进行注册。

[[UIApplication sharedApplication] 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 = [[super allocWithZone:NULL] 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 = [UIUserNotificationSettings settingsForTypes:types categories:nil]; if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; } #endif } -(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message { NSLog(@"addNotifiction"); UILocalNotification *localNotification = [[UILocalNotification alloc] init]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm:ss"]; NSDate *now = [formatter dateFromString:firedate];//触发通知的时间
  //如果firedate传入的是XX:XX:XX格式在表示在固定的时间点发送通知,如果传入的是XX格式表示从现在开始XX秒后发送通知 if(now == nil) { NSTimeInterval secs = [firedate doubleValue]; now = [NSDate dateWithTimeIntervalSinceNow:secs]; } localNotification.fireDate = now; //设置 时区 localNotification.timeZone = [NSTimeZone defaultTimeZone]; // 触发后,弹出警告框中显示的内容 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};
// 注册本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
/**
 removeLocalNotificationByKey
 */
- (void)removeLocalNotificationByKey:(NSString*)key {
    // 取出全部本地通知
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    // 设置要移除的通知id
    NSString *notificationId = key;
    // 遍历进行移除
    for (UILocalNotification *localNotification in notifications) {
        // 将每个通知的id取出来进行对比
        if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:notificationId]) {
            // 移除某一个通知
            [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
        }
    }
}

- (void)removeLocalAllNotification {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end

用法举例:

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

[[NotificationHelper shareInstance] registerLocalNotification:application];

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

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

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

下次有空再说。

关键词:兼容