//在iOS8系统一上加入这句话是允许用户发送本地通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
// Override point for customization after application launch.
return YES;
}
//取消通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// NSString *pathString = [[NSBundle mainBundle] pathForResource:@"滴答 - 侃侃" ofType:@"mp3"];
// AVAudioPlayer *avAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:pathString] error:nil];
// avAudio.delegate = self;
// [avAudio play];
application.applicationIconBadgeNumber = 0;
// [avAudio release];
}
//注册一个通知
UILocalNotification *notification=[[UILocalNotification alloc]init];
if (notification!=nil)
{
NSDate *now = [NSDate date];
//从现在开始,10秒以后通知
notification.fireDate=[now dateByAddingTimeInterval:50];
//使用本地时区
notification.timeZone=[NSTimeZone defaultTimeZone];
//通知重复提示的单位,可以是天、周、月
// notification.repeatInterval = NSCalendarUnitCalendar;
notification.alertBody=@"1111";
//通知提示音 使用默认的
notification.soundName=UILocalNotificationDefaultSoundName;
notification.alertAction=NSLocalizedString(@"你1111", nil);
//这个通知到时间时,你的应用程序右上角显示的数字。
notification.applicationIconBadgeNumber = 1;
//add key 给这个通知增加key 便于半路取消。nfkey这个key是我自己随便起的。
// 假如你的通知不会在还没到时间的时候手动取消 那下面的两行代码你可以不用写了。
NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:@"mm",@"nfkey",nil];
[notification setUserInfo:dict];
//启动这个通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}