- 論壇徽章:
- 0
|
如果要兼容IOS8在IOS中實(shí)現(xiàn)本地推送,關(guān)鍵是要注意:ios8在實(shí)現(xiàn)本地推送時(shí)需要通過(guò)如下語(yǔ)句進(jìn)行注冊(cè)。- [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
復(fù)制代碼 至于IOS8之前版本的做法就不多說(shuō)了,直接上代碼。新建oc類文件(NotificationHelper),在NotificationHelper.h中聲明相關(guān)方法如下:- #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
復(fù)制代碼 在NotificationHelper.m文件中實(shí)現(xiàn)方法如下:- #import "NotificationHelper.h"
- @implementation NotificationHelper
- static NotificationHelper* instance;
- //實(shí)現(xiàn)單例
- +(NotificationHelper*) shareInstance
- {
- static dispatch_once_t onceToken ;
- dispatch_once(&onceToken, ^{
- instance = [[super allocWithZone:NULL] init] ;
- });
- return instance ;
- }
- //推送處理[注冊(cè)消息通知]
- -(void) registerLocalNotification:(UIApplication*)application
- {
- application.applicationIconBadgeNumber = 0;//清除應(yīng)用圖標(biāo)上的數(shù)字
- //關(guān)鍵:加上版本的控制
- #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];//觸發(fā)通知的時(shí)間
- //如果firedate傳入的是XX:XX:XX格式在表示在固定的時(shí)間點(diǎn)發(fā)送通知,如果傳入的是XX格式表示從現(xiàn)在開(kāi)始XX秒后發(fā)送通知
- if(now == nil)
- {
- NSTimeInterval secs = [firedate doubleValue];
- now = [NSDate dateWithTimeIntervalSinceNow:secs];
- }
- localNotification.fireDate = now;
- //設(shè)置 時(shí)區(qū)
- localNotification.timeZone = [NSTimeZone defaultTimeZone];
- // 觸發(fā)后,彈出警告框中顯示的內(nèi)容
- localNotification.alertBody = message;
- localNotification.alertAction = NSLocalizedString(@"View Details", nil);
- // 觸發(fā)時(shí)的聲音(這里選擇的系統(tǒng)默認(rèn)聲音)
- localNotification.soundName = UILocalNotificationDefaultSoundName;
- // 觸發(fā)頻率(repeatInterval是一個(gè)枚舉值,可以選擇每分、每小時(shí)、每天、每年等)
- localNotification.repeatInterval = kCFCalendarUnitDay;//測(cè)試用暫時(shí)寫死為每隔一天 0:不重復(fù)
- // 需要在App icon上顯示的未讀通知數(shù)(設(shè)置為1時(shí),多個(gè)通知未讀,系統(tǒng)會(huì)自動(dòng)加1,如果不需要顯示未讀數(shù),這里可以設(shè)置0)
- localNotification.applicationIconBadgeNumber = 1;
- // 設(shè)置通知的id,可用于通知移除,也可以傳遞其他值,當(dāng)通知觸發(fā)時(shí)可以獲取
- localNotification.userInfo = @{@"id" : key};
- // 注冊(cè)本地通知
- [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
- [localNotification release];
- }
- /**
- removeLocalNotificationByKey
- */
- - (void)removeLocalNotificationByKey:(NSString*)key {
- // 取出全部本地通知
- NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
- // 設(shè)置要移除的通知id
- NSString *notificationId = key;
- // 遍歷進(jìn)行移除
- for (UILocalNotification *localNotification in notifications) {
- // 將每個(gè)通知的id取出來(lái)進(jìn)行對(duì)比
- if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:notificationId]) {
- // 移除某一個(gè)通知
- [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
- }
- }
- }
- - (void)removeLocalAllNotification {
- [[UIApplication sharedApplication] cancelAllLocalNotifications];
- }
- @end
復(fù)制代碼 用法舉例:
比如在應(yīng)用啟動(dòng)的時(shí)候調(diào)在didFinishLaunchingWithOptions方法中調(diào)用:
[[NotificationHelper shareInstance] registerLocalNotification:application];
進(jìn)行注冊(cè)和版本控制,在需要發(fā)送通知的時(shí)候調(diào)用:
[[NotificationHelper shareInstance] addNotifiction:"18:30:30" keyA:"key" messageA:"可以領(lǐng)取體力了!" ]
完畢。由于公司的手游項(xiàng)目需要使用到本地推送,而我們的項(xiàng)目是用quick cocos2d-x引擎,前端使用LUA編寫腳本和界面。這樣就面臨一個(gè)問(wèn)題:如何編寫友好的接口讓lua能夠調(diào)用oc來(lái)實(shí)現(xiàn)推送,這樣的話所有的邏輯都在lua中實(shí)現(xiàn)。
下次有空再說(shuō)。 |
|