第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展:
1.创建一个本地通知,添加到系统:
1 // 初始化本地通知对象 2 UILocalNotification *notification = [[UILocalNotification alloc] init]; 3 if (notification) { 4 // 设置通知的提醒时间 5 NSDate *currentDate = [NSDate date]; 6 notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区 7 notification.fireDate = [currentDate dateByAddingTimeInterval:5.0]; 8 9 // 设置重复间隔 10 notification.repeatInterval = kCFCalendarUnitDay; 11 12 // 设置提醒的文字内容 13 notification.alertBody = @"Wake up, man"; 14 notification.alertAction = NSLocalizedString(@"起床了", nil); 15 16 // 通知提示音 使用默认的 17 notification.soundName= UILocalNotificationDefaultSoundName; 18 19 // 设置应用程序右上角的提醒个数 20 notification.applicationIconBadgeNumber++; 21 22 // 设定通知的userInfo,用来标识该通知 23 NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc] init]; 24 aUserInfo[kLocalNotificationID] = @"LocalNotificationID"; 25 notification.userInfo = aUserInfo; 26 27 // 将通知添加到系统中 28 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 29 }
repeatInterval表示通知的重复间隔,在SDK中定义如下:
1 typedef CF_OPTIONS(CFOptionFlags, CFCalendarUnit) { 2 kCFCalendarUnitEra = (1UL << 1), 3 kCFCalendarUnitYear = (1UL << 2), 4 kCFCalendarUnitMonth = (1UL << 3), 5 kCFCalendarUnitDay = (1UL << 4), 6 kCFCalendarUnitHour = (1UL << 5), 7 kCFCalendarUnitMinute = (1UL << 6), 8 kCFCalendarUnitSecond = (1UL << 7), 9 kCFCalendarUnitWeek CF_ENUM_DEPRECATED(10_4, 10_10, 2_0, 8_0) = (1UL << 8), 10 kCFCalendarUnitWeekday = (1UL << 9), 11 kCFCalendarUnitWeekdayOrdinal = (1UL << 10), 12 kCFCalendarUnitQuarter CF_ENUM_AVAILABLE(10_6, 4_0) = (1UL << 11), 13 kCFCalendarUnitWeekOfMonth CF_ENUM_AVAILABLE(10_7, 5_0) = (1UL << 12), 14 kCFCalendarUnitWeekOfYear CF_ENUM_AVAILABLE(10_7, 5_0) = (1UL << 13), 15 kCFCalendarUnitYearForWeekOfYear CF_ENUM_AVAILABLE(10_7, 5_0) = (1UL << 14), 16 };
这里比较不好的一点是该值不能自定义(很遗憾,NSCalendarUnit是个枚举类型),例如你不能塞个10.0给它从而希望它每十秒重复一次。所以如果你想每20分钟发送一次通知,一小时内发送3次,那么只能同时设定三个通知了。
2.收到通知后委托内的自定义实现:
1 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ 2 NSLog(@"Application did receive local notifications"); 3 4 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"welcome" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 5 [alert show]; 6 }
3.如何取消(删除本地通知)
有一点需要注意,如果我们的应用程序给系统发送的本地通知是周期性的,那么即使把程序删了重装,之前的本地通知在重装时依然存在(没有从系统中移除)。
因此我们需要取消通知的方法,当然该对象也会在scheduledLocalNotifications数组中移除。
取消方法分为两种。
第一种比较暴力,直接取消所有的本地通知:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
这个适合在App重装时第一次启动的时候,或还原程序默认设置等场合下使用。
第二种方法是针对某个特定通知的:
- (void)cancelLocalNotification:(UILocalNotification *)notification
4.如何标识一个本地通知
需要通知有一个标识,这样我们才能定位是哪一个通知。可以在notification的userInfo(一个字典)中指定。
例如:
1 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ 2 NSLog(@"Application did receive local notifications"); 3 4 // 取消某个特定的本地通知 5 for (UILocalNotification *noti in [[UIApplication sharedApplication] scheduledLocalNotifications]) { 6 NSString *notiID = noti.userInfo[kLocalNotificationID]; 7 NSString *receiveNotiID = notification.userInfo[kLocalNotificationID]; 8 if ([notiID isEqualToString:receiveNotiID]) { 9 [[UIApplication sharedApplication] cancelLocalNotification:notification]; 10 return; 11 } 12 } 13 14 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"welcome" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 15 [alert show]; 16 }
当然使用上述本地通知的前提是,应用获取到了系统的通知权限,需要注册通知:
注册之前有两个前提条件必须准备好:
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 UIDevice *device = [UIDevice currentDevice]; 3 float sysVersion = [device.systemVersion floatValue]; 4 if (sysVersion >= 8.0f) { 5 UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; 6 if (UIUserNotificationTypeNone != setting.types) {
[self addLocalNotification]; 7 NSLog(@"已经允许了通知"); 8 }else{ 9 [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; 10 [[UIApplication sharedApplication] registerForRemoteNotifications]; 11 } 12 }else{ 13 UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 14 if(UIRemoteNotificationTypeNone != type){ 15 NSLog(@"已经允许了通知"); 16 }else{ 17 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 18 } 19 } 20 21 //[self registerRemoteNotification]; 22 return YES; 23 } 24 25 #pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行) 26 -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ 27 if (notificationSettings.types!=UIUserNotificationTypeNone) { 28 //[self addLocalNotification]; 29 NSLog(@"允许了通知"); 30 } 31 } 32 33 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 34 NSLog(@"注册成功"); 35 } 36 37 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 38 NSLog(@"注册失败"); 39 }
注:
从上面的程序可以看到userInfo这个属性我们设置了参数,那么这个参数如何接收呢?
在iOS中如果点击一个弹出通知(或者锁屏界面滑动查看通知),默认会自动打开当前应用。由于通知由系统调度那么此时进入应用有两种情况:
当然如果是后者自然不必多说,因为参数中已经可以拿到notification对象,只要读取userInfo属性即可。如果是前者的话则可以访问launchOptions中键为UIApplicationLaunchOptionsLocalNotificationKey的对象,这个对象就是发送的通知,由此对象再去访问userInfo。为了演示这个过程在下面的程序中将userInfo的内容写入文件以便模拟关闭程序后再通过点击通知打开应用获取userInfo的过程。
1 #pragma mark - 应用代理方法 2 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 3 //添加通知 4 [self addLocalNotification]; 5 6 //接收通知参数 7 UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 8 NSDictionary *userInfo= notification.userInfo; 9 10 [userInfo writeToFile:@"/Users/kenshincui/Desktop/didFinishLaunchingWithOptions.txt" atomically:YES]; 11 NSLog(@"didFinishLaunchingWithOptions:The userInfo is %@.",userInfo); 12 13 return YES; 14 }
上面的程序可以分为两种情况去运行:
两种情况会分别按照前面说的情况调用不同的方法接收到userInfo写入本地文件系统。有了userInfo一般来说就可以根据这个信息进行一些处理,例如可以根据不同的参数信息导航到不同的界面,假设是更新的通知则可以导航到更新内容界面等。