1、推送通知的2种方式
1)本地推送通知(Local Notification)
2)远程推送通知(Remote Notification)
2、通知的作用
可以让不在前台运行的app,告知用户app内部发生了什么事情
3、使用场景
常用来定时提醒用户完成一些任务,比如清理垃圾、记账、买衣服、看电影、玩游戏
4、推送通知的5中不同的效果
1)在屏幕顶部显示一块横幅(显示具体内容)
2)在屏幕中间弹出一个UIAlertView(显示具体内容)
3)在锁屏界面显示一块横幅(锁屏状态下,显示具体内容)
4)更新app图标的数字(说明新内容的数量)
5)播放音效(提醒作用)
5、推送通知的主要事项
1)发出推送通知时,如果当前程序正运行在前台,那么推送通知就不会被呈现出来
2)点击推送通知后,默认会自动打开发出推送通知的app
3)不管app打开还是关闭,推送通知都能如期发出
6、什么是本地推送和应用场景
本地推送:就是不需要联网就能发出的推送通知(不需要服务器的支持)
应用场景:常用来定时提醒用户完成一些任务,比如清理垃圾、记账、买衣服、看电影、玩游戏
下面在代码中分析一下本地通知的各个属性:
1.在AppDelegate的.m文件中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1.iOS8需要添加请求用户的授权
if ([UIDevice currentDevice].systemVersion.floatValue >=8.0) {
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
return YES;
}
2.在VC的.m文件中
// 1.创建本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.设置什么时间弹出
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.设置弹出的内容
localNote.alertBody = @"CN EZ";
// 1.3.设置锁屏状态下,显示的一个文字
localNote.alertAction = @"DOTA2";
// 1.4.显示启动图片
//localNote.alertLaunchImage = @"123";
// 1.5.是否显示alertAction的文字(默认是YES)
localNote.hasAction = YES;
// 1.6.设置音效
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.7.应用图标右上角的提醒数字
localNote.applicationIconBadgeNumber = 999;
// 1.8.设置UserInfo来传递信息
// localNote.userInfo = @{@"alertBody" : localNote.alertBody, @"applicationIconBadgeNumber" : @(localNote.applicationIconBadgeNumber)};
// 2.调度通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
3、其余属性的简单介绍:
@property(nonatomic,copy) NSDate *fireDate; // 设置本地推送的时间
@property(nonatomic,copy) NSTimeZone *timeZone; // 时区
@property(nonatomic) NSCalendarUnit repeatInterval; // 重复多少个单元发出一次
@property(nonatomic,copy) NSCalendar *repeatCalendar; // 设置日期
@property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); // 比如某一个区域的时候发出通知
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); // 进入区域是否重复
// user info
@property(nonatomic,copy) NSDictionary *userInfo;
此次Demo的源码下载:https:///fengzhihao123/LocalNotification