开发中不单单有远程推送还有本地通知,这样能够减轻服务器的压力。服务器
1、在IOS8中须要先注册app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; return YES; }
2、在-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法中接收通知ide
//这个方法只有在程序启动以后才会执行,所以当程序处于后台时,该方法不会执行 -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"%@",notification); UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"通知" message:@"下班了" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; [alertView show]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; }
3、建立本地通知ui
// // ViewController.m // UILocalNotification // // Created by City--Online on 15/5/15. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILocalNotification *notification=[[UILocalNotification alloc]init]; if (notification) { //设置推送时间 NSDate *currentDate=[NSDate date]; notification.timeZone=[NSTimeZone defaultTimeZone]; notification.fireDate=[currentDate dateByAddingTimeInterval:10]; //设置重复间隔 NSCalendarUnit类型 0表示不重复 notification.repeatInterval=kCFCalendarUnitMinute; //设置提醒的文字 //设备收到本地通知时横额或锁屏时的主要文字内容 notification.alertBody=@"内容"; notification.alertTitle=@"标题"; //锁屏时显示的slide to后面的文字内容 notification.alertAction=NSLocalizedString(@"我是谁?", nil); //提示音 默认 notification.soundName=UILocalNotificationDefaultSoundName; //设置应用程序右上角的提醒个数 notification.applicationIconBadgeNumber++; //设置通知的userInfo NSMutableDictionary *userInfo=[[NSMutableDictionary alloc]init]; [userInfo setObject:@"kLocalNotificationID" forKey:@"kLocalNotificationID"]; [userInfo setObject:@"cuiyanwei" forKey:@"name"]; notification.userInfo=userInfo; //将通知添加到系统中 //若是咱们的应用程序给系统发送的本地通知是周期性的,那么即便把程序删了重装,以前的本地通知在重装时依然存在(没有从系统中移除)(注册到系统服务里并非在APP中) [[UIApplication sharedApplication]scheduleLocalNotification:notification]; // [self delete]; } } //删除本地通知 -(void)delete { //往系统服务里面注册以后有时会须要删除 //1.暴力删除,取消全部的通知 //[[UIApplication sharedApplication] cancelAllLocalNotifications]; //2.删除制定的通知 for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) { NSString *kLocalNotificationID=[notification.userInfo objectForKey:@"kLocalNotificationID"]; NSLog(@"%@",kLocalNotificationID); if ([kLocalNotificationID isEqualToString:@"kLocalNotificationID"]) { [[UIApplication sharedApplication] cancelLocalNotification:notification]; UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"取消通知" message:@"已经取消通知" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:nil, nil]; [alertView show]; break; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
4、运行结果3d