概述:
在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情。ios中通知机制又叫消息机制,其包括两类:一类是本地通知;另一类是推送通知。也叫远程通知。两种通知在ios中表现一致,可以通过横幅或者弹出提醒两种形式告诉用户,并且点击通知可以打开应用程序,但是实现原理却完全不同。今天就和大家一块去看一下本地通知,远程通知下回分享。
本地通知:
本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时,代办事项,又或者一个应用在一段时间内不使用又提示用户使用此应用等都是本地通知。
ios8之后要在applegate中注册本地通知,否则本地通知不可用。
要点:ios8后推出了UILocalNotification 本地通知,它的用法如
applegate.m中
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:setting];
}
发送本地通知的方法为
/*
// 创建本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.设置通知发出的时间
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.设置通知内容
localNote.alertBody = @"这是一个推送这是一个推送";
localNote.alertTitle = @"hello world";
// 1.3.设置锁屏时,字体下方显示的一个文字
localNote.alertAction = @"赶紧!!!!!";
localNote.hasAction = YES;
// 1.4.设置启动图片(通过通知打开的)
// localNote.alertLaunchImage = @"../Documents/IMG_0024.jpg";
// 1.5.设置通过到来的声音
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.6.设置应用图标左上角显示的数字
localNote.applicationIconBadgeNumber = 999;
// 1.7.设置一些额外的信息
localNote.userInfo = @{@"qq" : @"704711253", @"msg" : @"success"};
// 2.执行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
*/
注意点:不管任何通知都是程序在后台或者杀死情况下,以通知栏的形式展示给用户,在前台是看不到通知栏的,当初作者就傻啦吧唧在前台发送通知,迟迟不出来通知,以为写错了,后来发现前台是不显示通知栏的,哈哈,
1、在applegate中有个方法为
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
当在前台收到通知会走这个方法, 还有当程序在后台时,收到通知,然后点击通知栏,也会调这个方法
这个方法一般会这样写,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"收到本地通知");
NSLog(@"---%@---",notification.alertBody);
if (application.applicationState == UIApplicationStateActive) {
UIAlertController *vc = [UIAlertController alertControllerWithTitle:notification.alertTitle message:notification.alertBody preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alerAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[vc addAction:alerAction];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
NSLog(@"---%ld---",application.applicationState);
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge --;
badge = badge >= 0 ? badge : 0;
application.applicationIconBadgeNumber = badge;
[application cancelAllLocalNotifications];
}
注意点是:程序从后台通过点击通知栏调以上方法时,此时程序状态是非活跃的,程序在前台收到通知时,程序是出于活跃状态
到ios10时,苹果推出新型推送通知框架 #import <UserNotifications/UserNotifications.h> 虽然推出了新型框架来代替之前的UILocalNotification,但是为了兼容市面上不同的苹果系统,UILocalNotification还是可以用
然后说说新型推送通知框架 #import <UserNotifications/UserNotifications.h>
注册通知
if ([UIDevice currentDevice].systemVersion.doubleValue >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions type = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
} else {
NSLog(@"注册通知失败");
}
}];
}
和上面的注册通知相似,而也有很大的不同,类名变了很多,里面提到了个用户通知中心新的概念,里面也判断了用户是否授权本地通知, 里面还设置了代理,到下面再说
发送本地通知的方法为
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"hello";
content.body = @"这是一个推送";
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge++;
content.badge = @(badge);
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"你好" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];