iOS开发交流群:301058503
1、创建本地通知
UIApplication * application=[UIApplication sharedApplication];
//如果当前应用程序没有注册本地通知,需要注册
if([application currentUserNotificationSettings].types==UIUserNotificationTypeNone){
//设置提示支持的提示方式
// UIUserNotificationTypeBadge 提示图标
// UIUserNotificationTypeSound 提示声音
// UIUserNotificationTypeAlert 提示弹框
UIUserNotificationSettings * setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:setting];
}
//删除之前的重复通知
[application cancelAllLocalNotifications];
//添加本地通知
NSDate * date=[NSDate dateWithTimeIntervalSinceNow:];//从现在开始3秒后发送
UILocalNotification * noti=[[UILocalNotification alloc] init];
NSDictionary *dict=[NSDictionary dictionaryWithObject:@"1" forKey:@"id"];
noti.userInfo=dict;
//设置开始时间
noti.fireDate=date;
//设置body
noti.alertBody[email protected]"第一条";//显示的内容
//设置action
noti.alertAction[email protected]"详情";
//设置闹铃
noti.soundName[email protected]"4195.mp3";
//注册通知
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
2、删除某个本地通知
NSArray *notificaitons = [UIApplication sharedApplication].scheduledLocalNotifications;//获取当前所有的本地通知
if (!notificaitons || notificaitons.count <= ) {
return;
}
for (UILocalNotification *notify in notificaitons) {
if ([[notify.userInfo objectForKey:@"id"] isEqualToString:@"1"]) {
//取消一个特定的通知
[[UIApplication sharedApplication] cancelLocalNotification:notify];
break;
}
}
本地通知是有数量限制的,一个应用可同时存在的本地通知数量为64个,包括发送的和未发送的,所以要及时处理掉已经过期的通知,否则用着用着就发现通知没用了