本地推送通知
对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。
app没有彻底关闭时
- app在前台时(包括待激活状态):可以接收通知,但不显示
- app在后台:可以接收,并显示通知
app彻底关闭时
- 点击程序图标:进入app主界面
- 点击通知:进入详情页面
- 不需要联网就能发出的推送通知(不需要服务器的支持)
- 常用来定时提醒用户完成一些任务,即使程序被杀死提示照样有,例如:记事本、备忘录等
1 获取通知授权
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:userSettings];
return YES;
}
2 创建通知对象(在需要的方法函数中写入)
//1.通知对象
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
//2.通知的时间
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
//3.通知的内容
localNotification.alertBody = @"HELLO IS ME";
//4.设置通知的时候的附加信息(非必须)
localNotification.userInfo = @{@"type":@"1",@"hello":@"word"};
//5.调度本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
监听本地推送通知
1 app没有彻底关闭时
//收到本地通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
if (application.applicationState == UIApplicationStateInactive){
NSLog(@"我是待激活状态来的");
if ([notification.userInfo[@"type"] isEqualToString:@"1"]) {
NSLog(@"做自己的事");//type是我的userinfo的里面的信息
}
}else if (application.applicationState == UIApplicationStateBackground){
//注:此处不会进来!!!,仅此状态下可以显示顶部通知提示,点通知会进到UIApplicationStateInactive方法(程序不挂的情况下)
NSLog(@"我是后台来的");
}else{//UIApplicationStateActive
NSLog(@"我是前台来的");
}
}
-
UIApplicationStateActive
应用在前台运行时就是这个状态 -
UIApplicationStateInactive
待激活状态:下拉通知栏 、锁屏、双击Home键、上拉任务栏等 -
UIApplicationStateBackground
后台状态:按Home键 、其他应用程序把应用挤入后台
2 app彻底关闭时
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:userSettings];
if(launchOptions){
UILocalNotification *localNote = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if ([localNote.userInfo[@"type"] isEqualToString:@"1"] ) {
NSLog(@"程序挂了之后点击通知栏进来的");
}
}
return YES;
}
说明
当应用程序启动时执行,应用程序启动入口。只在应用程序启动时执行一次。
@param application 用来获取应用程序的状态、变量等
@param launchOptions 该参数存储程序启动的原因
1 若用户直接启动,lauchOptions内无数据
2 若由其他应用程序通过openURL:启动 则UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString)
3 若由本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification)
4 若由远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo(NSDictionary
5 其他key还有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,UIApplicationLaunchOptionsNewsstandDownloadsKey。 如果要在启动时,做出一些区分,那就需要在下面的代码做处理。 比如:应用可以被某个其它应用调起(作为该应用的子应用),要实现单点登录,那就需要在启动代码的地方做出合理的验证,并跳过登录。
@return return value description
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
if(url) {
}
NSString *bundleId = [launchOptions objectForKey:UIApplicationLaunchOptionsSourceApplicationKey];
if(bundleId) {
}
UILocalNotification * localNotify = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if(localNotify) {
}
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo) {
}
return YES;
}
取消本地通知
//取消某一个通知
NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
//获取当前所有的本地通知
if (!notificaitons || notificaitons.count <= 0) {
return;
}
for (UILocalNotification *notify in notificaitons) {
if ([[notify.userInfo objectForKey:@"type"] isEqualToString:@"1"]) {
//取消一个特定的通知
[[UIApplication sharedApplication] cancelLocalNotification:notify];
break;
}
}
//取消所有的本地通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
留下的问题:程序后台运行或者挂掉的情况下,不点击通知如何拿到数据?是不是就丢了该通知数据...