创建通知
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
NSDate *now = [NSDate new];
notification.fireDate = [now dateByAddingTimeInterval:10]; //10秒后通知
notification.repeatInterval=0; //重复次数,kCFCalendarUnitWeekday一周一次
notification.timeZone = [NSTimeZone defaultTimeZone]; //设置时区
notification.applicationIconBadgeNumber = 1; //应用的角标
notification.soundName = UILocalNotificationDefaultSoundName; //声音,可以换成alarm.soundName = @"sound.wav"
//去掉下面2行就不会弹出提示框
notification.alertBody = @"通知内容"; //提示信息 弹出提示框
notification.alertAction = @"打开"; //提示框按钮
//notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
//设置userinfo 方便在之后需要撤销的时候使用 也可以传递其他值,当通知触发时可以获取
notification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
推送的内容
//推送的内容
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
{
//这里,你就可以通过notification的useinfo,干一些你想做的事情了
if ([[notification.userInfo objectForKey:@"key"] isEqualToString:@"value"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"你的系统存在严重威胁" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil,nil];
[alert show];
}
application.applicationIconBadgeNumber = 0; //移除角标
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//不通过推送 通过应用图标打开应用 移除角标
application.applicationIconBadgeNumber = 0;
}
取消通知
//获取当前所有的本地通知
NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
if (!notificaitons || notificaitons.count <= 0)
{
return;
}
//取消一个特定的通知
for (UILocalNotification *notify in notificaitons)
{
if ([[notify.userInfo objectForKey:@"key"] isEqualToString:@"value"])
{
[[UIApplication sharedApplication] cancelLocalNotification:notify];
break;
}
}
// //取消所有的本地通知
// [[UIApplication sharedApplication] cancelAllLocalNotifications];