UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { NSDate *now = [NSDate date]; //從現在開始,10秒以後通知 notification.fireDate=[now dateByAddingTimeInterval:10];
//notification.fireDate = [NSDate dateWithTimeIntervalSince1970:8*60*60];//8點通知 //使用本地時區 notification.timeZone=[NSTimeZone defaultTimeZone]; notification.alertBody=@"頂部提示內容,通知時間到啦"; //通知提示音 使用默認的 notification.soundName= UILocalNotificationDefaultSoundName; notification.alertAction=NSLocalizedString(@"你鎖屏啦,通知時間到啦", nil); //這個通知到時間時,你的應用程序右上角顯示的數字。 notification.applicationIconBadgeNumber = 1; //add key 給這個通知增加key 便於半路取消。nfkey這個key是我自己隨便起的。 // 假如你的通知不會在還沒到時間的時候手動取消 那下面的兩行代碼你可以不用寫了。 NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:notificationtag],@"nfkey",nil]; [notification setUserInfo:dict]; //啓動這個通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; //這句真的特別特別重要。如果不加這一句,通知到時間了,發現頂部通知欄提示的地方有了,然後你通過通知欄進去,然後你發現通知欄裏邊還有這個提示 //除非你手動清除,這當然不是我們希望的。加上這一句就好了。網上很多代碼都沒有,就比較鬱悶了。 [notification release];
}
//每1分鐘提醒一次
notification.repeatInterval= NSMinuteCalendarUnit;
//每1小時提醒一次
notification.repeatInterval= NSHourCalendarUnit;
//每1天提醒一次
notification.repeatInterval=NSDayCalendarUnit;