iOS本地推送UILocalNotification很方便的实现日程提醒,生日提醒等功能,不需要服务器在本地就可以代码实现。iOS8之后需要代码注册否则不能发送信息!
1.本地推送注册
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"action";//按钮的标示
action.title=@"Accept";//按钮的标题
action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
// action.authenticationRequired = YES;
// action.destructive = YES;
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按钮
action2.identifier = @"action2";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
action.authenticationRequired = YES;//需要解锁才能处理,如
action2.activationMode = UIUserNotificationActivationModeForeground;
action.destructive = YES;
//2.创建动作(按钮)的类别集合
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";//这组动作的唯一标示
[categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
//3.创建UIUserNotificationSettings,并设置消息的显示类类型
UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:notiSettings];
}
2.本地发送信息
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @"测试推送的快捷回复";//alertBody是设备收到本地通知时横额或锁屏时的主要文字内容
notification.alertAction = @"现在开启神秘之旅吧!";//alertActions是锁屏时显示的slide to后面的文字内容
notification.hasAction = YES;//是否显示额外的按钮,为no时alertAction消失
notification.category = @"alert";
notification.repeatInterval = kCFCalendarUnitSecond;//循环的周期
notification.soundName = UILocalNotificationDefaultSoundName;
/**自定义通知声:直接加至项目里面即可notification.soundName = @“要播放的文件名”;
注意:1.音频文件时长需要小于30秒的。
2.格式一定要支持播放,常用的格式caf
**/
notification.userInfo=[[NSDictionary alloc] initWithObjectsAndKeys:@"ww",@"ee", nil];
notification.applicationIconBadgeNumber = 1;// 需要在App icon上显示的未读通知数(设置为1时,多个通知未读,系统会自动加1,如果不需要显示未读数,这里可以设置0)
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
3.删除本地推送
// 取出全部本地通知
NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
// 设置要移除的通知id
NSString *notificationId = @"ee";
// 遍历进行移除
for (UILocalNotification *localNotification in notifications) {
// 将每个通知的id取出来进行对比
if ([[localNotification.userInfo objectForKey:@"ww"] isEqualToString:notificationId]) {
// 移除某一个通知
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
// [[UIApplication sharedApplication] cancelAllLocalNotifications];删除所有
}
}
4.推送处理
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app"
message:notification.alertBody
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
//这里,你就可以通过notification的useinfo,干一些你想做的事情了
application.applicationIconBadgeNumber -= 1;
}
代码下载: 点击打开链接