1、增加一個本地推送
//設置20秒之后
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];
//chuagjian一個本地推送
UILocalNotification *noti = [[UILocalNotification alloc] init];
if (noti) {
//設置推送時間
noti.fireDate = date;
//設置時區(qū)
noti.timeZone = [NSTimeZone defaultTimeZone];
//設置重復間隔
noti.repeatInterval = NSWeekCalendarUnit;
//推送聲音
noti.soundName = UILocalNotificationDefaultSoundName;
//內容
noti.alertBody = @"推送內容";
//顯示在icon上的紅色圈中的數子
noti.applicationIconBadgeNumber = 1;
//設置userinfo 方便在之后需要撤銷的時候使用
NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
noti.userInfo = infoDic;
//添加推送到uiapplication
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:noti];
}
2矗烛、程序運行時接收到本地推送消息
- (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;
}
3富寿、取消一個本地推送
UIApplication *app = [UIApplication sharedApplication];
//獲取本地推送數組
NSArray *localArr = [app scheduledLocalNotifications];
//聲明本地通知對象
UILocalNotification *localNoti;
if (localArr) {
for (UILocalNotification *noti in localArr) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:@"key"];
if ([inKey isEqualToString:key]) {
if (localNoti){
[localNoti release];
localNoti = nil;
}
localNoti = [noti retain];
break;
}
}
}
//判斷是否找到已經存在的相同key的推送
if (!localNoti) {
//不存在 初始化
localNoti = [[UILocalNotification alloc] init];
}
if (localNoti && !state) {
//不推送 取消推送
[app cancelLocalNotification:localNoti];
[localNoti release];
return;
}
}