iOS 8 對本地推送的註冊方式做了調整,兼容的關鍵在於:必須先通過 registerUserNotificationSettings: 註冊用戶允許的通知類型。
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
下面把註冊、添加、移除三部分封裝成一個 NotificationHelper 單例。
#import <UIKit/UIKit.h> @interface NotificationHelper : NSObject - (void)addNotifiction:(NSString *)firedate keyA:(NSString *)key messageA:(NSString *)message; - (void)removeLocalNotificationByKey:(NSString *)key; - (void)removeLocalAllNotification; - (void)registerLocalNotification:(UIApplication *)application; + (NotificationHelper *)shareInstance; @end
#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;
}
#pragma mark - 註冊通知權限
- (void)registerLocalNotification:(UIApplication *)application {
application.applicationIconBadgeNumber = 0; // 清掉圖標角標
// 版本判斷,避免在 iOS 8 以下調用新 API
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
#endif
}
#pragma mark - 添加本地通知
- (void)addNotifiction:(NSString *)firedate keyA:(NSString *)key messageA:(NSString *)message {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
NSDate *now = [formatter dateFromString:firedate];
// 傳入 HH:mm:ss 表示在固定時刻觸發;傳入純數字則表示多少秒後觸發
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;
localNotification.repeatInterval = kCFCalendarUnitDay; // 0 表示不重複
localNotification.applicationIconBadgeNumber = 1; // 圖標角標數
localNotification.userInfo = @{@"id": key}; // 自定義標識,便於後續移除
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
#pragma mark - 移除通知
/** 按 key 移除某一條 */
- (void)removeLocalNotificationByKey:(NSString *)key {
NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *localNotification in notifications) {
if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:key]) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
}
}
}
/** 移除全部 */
- (void)removeLocalAllNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end
在 application:didFinishLaunchingWithOptions: 中完成註冊:
[[NotificationHelper shareInstance] registerLocalNotification:application];
需要發送通知時:
[[NotificationHelper shareInstance] addNotifiction:@"18:30:30" keyA:@"key" messageA:@"可以領取體力了!"];
由於公司手游項目使用 quick cocos2d-x 引擎、前端用 Lua 編寫腳本與界面,接下來要解決的是如何設計一組友好接口讓 Lua 能調用 Objective-C 完成推送,從而把邏輯都收在 Lua 層。
這套 API 基於 UILocalNotification,在 iOS 10 之後已被 UserNotifications 框架取代,新項目請直接使用 UNUserNotificationCenter。另外代碼中的 [localNotification release] 屬於 MRC 寫法,ARC 工程下需刪除。MC 與 ARC 混用時,可以在文件編譯標誌中加上 -fno-objc-arc 讓該文件按 MRC 編譯。