在iOS中,有两种推送类型,一种是远程推送,通过apns推送。另一种是本地推送UILocalNotification.本章介绍本地通知。
- (void) creatLocalNotification{
//当前时间的20s之后
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
UILocalNotification *notifacation = [[UILocalNotification alloc] init ];
if (notifacation) {
//timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发
[notifacation setTimeZone:[NSTimeZone defaultTimeZone]];
//fireDate是UILocalNotification的激发的确切时间
[notifacation setFireDate:date];
//repeatInterval是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位(NSCalendarUnit)的,例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发
[notifacation setRepeatInterval:NSWeekdayCalendarUnit];
//推送内容
[notifacation setAlertBody:@"推送demo"];
//推送声音
notifacation.soundName = UILocalNotificationDefaultSoundName;
//设置badge
[notifacation setApplicationIconBadgeNumber:1];
notifacation.alertLaunchImage = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"nit",@"key", nil];
notifacation.userInfo = dic;
UIApplication *application = [UIApplication sharedApplication];
[application scheduleLocalNotification:notifacation];
}
/**
UILocalNotification *notifacation1 = [[UILocalNotification alloc] init ];
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:15];
if (notifacation1) {
//timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发
[notifacation1 setTimeZone:[NSTimeZone defaultTimeZone]];
//fireDate是UILocalNotification的激发的确切时间
[notifacation1 setFireDate:date1];
//repeatInterval是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位(NSCalendarUnit)的,例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发。说明白就是 每周触发一次 还是每天 还是每年,,每月。都有对应的宏可供选择
[notifacation1 setRepeatInterval:NSWeekdayCalendarUnit];
//推送内容
[notifacation1 setAlertBody:@"推送demo"];
//推送声音
notifacation1.soundName = UILocalNotificationDefaultSoundName;
//设置badge
[notifacation1 setApplicationIconBadgeNumber:1];
// [notifacation setAlertAction:@"取消"];
// [notifacation setAlertLaunchImage:@"1.png"];
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"nit1",@"key", nil];
notifacation1.userInfo = dic;
UIApplication *application = [UIApplication sharedApplication];
[application scheduleLocalNotification:notifacation1];
}
*/
}
对于多个本地通知,如何区分呢,如上代码已经写出了
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"nit1",@"key", nil];
notifacation1.userInfo = dic;
用一个字典 , 写入作为区分的关键字。
代理方法:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"推送"
message:notification.alertBody
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:nil, nil];
[alert show];
//badge减1
application.applicationIconBadgeNumber -= 1;
}
用 此方法接受本地通知的回调。
取消UILocalNotifacation
- (void) cancelNotification{
//1
NSArray *localArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *not in localArray) {
if (not.userInfo) {
if([[not.userInfo objectForKey:@"key"] isEqualToString:@"nit"] ){
[[UIApplication sharedApplication] cancelLocalNotification:not];
}
if([[not.userInfo objectForKey:@"key"] isEqualToString:@"nit1"] ){
[[UIApplication sharedApplication] cancelLocalNotification:not];
}
}
}
//2,取消一个本地通知
[[UIApplication sharedApplication] cancelLocalNotification:nil];
//3,取消所有本地通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}