在iOS中 相对于远程通知 本地通知要简单得多 只有两个地方写点代码就能实现
本地通知(UILocalNotification) 操作流程
1.接收通知(接收通知对象)
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// 接收通知操作
}
2.注册发送通知(需注意iOS8之后的改动)
- (void)pushNotification
{
// 注册发送通知的方法
// 初始化 UILocalNotification alloc init
UILocalNotification *not = [[UILocalNotification alloc]init];
// 设置启动时间
not.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// 设置通知的标题
not.alertTitle = @"时间到";
// 设置通知的内容
not.alertBody = @"起床敲代码";
// 通过通知 传递内容
not.userInfo = @{@"key":@"value"};
// 设置app图标上面红点显示的数字
not.applicationIconBadgeNumber = 1;
// 注册通知
// iOS8之前
not.soundName = UILocalNotificationDefaultSoundName;
// NSCalendarUnitEra = kCFCalendarUnitEra,
// NSCalendarUnitYear = kCFCalendarUnitYear,
// NSCalendarUnitMonth = kCFCalendarUnitMonth,
// NSCalendarUnitDay = kCFCalendarUnitDay,
// NSCalendarUnitHour = kCFCalendarUnitHour,
// NSCalendarUnitMinute = kCFCalendarUnitMinute,
// NSCalendarUnitSecond = kCFCalendarUnitSecond,
// NSCalendarUnitWeekday = kCFCalendarUnitWeekday,
// NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
not.repeatInterval = NSCalendarUnitDay;
// 判断是否可以响应某个方法
// [self respondsToSelector:<#(SEL)#>]
// UIUserNotificationTypeBadge| 圆圈内提示的数字
// UIUserNotificationTypeSound| 通知提示的声音
// UIUserNotificationTypeNone|
// UIUserNotificationTypeAlert 振动
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
}
// 发送通知
[[UIApplication sharedApplication]scheduleLocalNotification:not];
}
//接收本地通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// 接收通知操作
NSLog(@"通知内容 %@",notification.userInfo);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:notification.alertTitle message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}