在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];
}