iOS 10 添加本地推送(Local Notification)
iOS 10 之前的本地推送
一,本地推送:
1,前台状态:
iOS10之后:前台状态下能够收到推送的弹窗口
iOS10之前:前台状态下能收到推送的消息,但是不能够收到推送的弹窗;不过,依然能够触发- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {}
方法
2,后台状态:
收到推送都能够收到推送的弹窗口
点击弹窗触发方法:
1,前台和后台
需要注意的是,前台收到推送会触发
方法一
,不需要点击进行点击推送内容的操作
// 方法一
// iOS 10 之前 -- 前台收到推送会直接调用这个方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
}
// 方法二
// iOS 10 之后 -- 这里使用的是极光推送
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
}
2,软件关闭状态
launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey取出本地推送通知对象
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}
二,本地推送实现
1,注册本地推送消息
在iOS 8.0 +,如果要使用本地通知,需要得到用户的许可
iOS 8.2之后,可以设置通知的弹框标题
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
```code
/** 注册本地推送 */
+ (void)registerLocalNotification:(NSInteger)alertTime alertTitle:(NSString *)aletTitle string:(NSString *)string userInfo:(NSDictionary *)userInfo{
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 设置触发通知的时间
//需要使用时间戳
NSDate *fireDate = [NSDate dateWithTimeIntervalSince1970:alertTime];
notification.fireDate = fireDate;
// 时区
notification.timeZone = [NSTimeZone defaultTimeZone];
// 设置重复的间隔
notification.repeatInterval = 0;//0表示不重复
// 通知内容
// 设置通知的弹框标题(iOS 8.2之后)
if([UIDevice currentDevice].systemVersion.floatValue >= 8.2) {
notification.alertTitle = aletTitle;
}
notification.alertBody = string;
notification.applicationIconBadgeNumber = 1;
// 通知被触发时播放的声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知参数
notification.userInfo = userInfo;
// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
// 执行通知注册
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
2,取消本地推送
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
// 获取所有本地通知数组
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
// 根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key];
// 如果找到需要取消的通知,则取消
if (info != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
break;
}
}
}
}
3,通知显示文本框
见 http://www.jianshu.com/p/ebe0879f18e0 (通知显示文本框(iOS 9.0之后才能使用))
属性介绍
// 实力本地推送对象
UILocalNotification *ln= [[UILocalNotificationalloc] init];
// 属性
推送通知的触发时间(何时发出推送通知)
@property(nonatomic,copy) NSDate *fireDate;
推送通知的具体内容
@property(nonatomic,copy) NSString *alertBody;
在锁屏时显示的动作标题(完整标题:“滑动来”+ alertAction)
@property(nonatomic,copy) NSString *alertAction;
音效文件名
@property(nonatomic,copy) NSString *soundName;
app图标数字
@property(nonatomic) NSIntegerapplicationIconBadgeNumber;
---
调度本地推送通知(调度完毕后,推送通知会在特地时间fireDate发出)
[[UIApplicationsharedApplication] scheduleLocalNotification:ln];
获得被调度(定制)的所有本地推送通知(已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)
@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
// NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
取消调度本地推送通知
- (void)cancelLocalNotification:(UILocalNotification*)notification;
- (void)cancelAllLocalNotifications;
立即发出本地推送通知
- (void)presentLocalNotificationNow:(UILocalNotification*)notification;
---
每隔多久重复发一次推送通知
@property(nonatomic) NSCalendarUnit repeatInterval;
点击推送通知打开app时显示的启动图片
@property(nonatomic,copy) NSString *alertLaunchImage;
附加的额外信息
@property(nonatomic,copy) NSDictionary *userInfo;
时区(一般设置为[NSTimeZonedefaultTimeZone],跟随手机的时区)
@property(nonatomic,copy) NSTimeZone *timeZone;
网友评论