博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
推送通知(一)本地通知
阅读量:4348 次
发布时间:2019-06-07

本文共 4166 字,大约阅读时间需要 13 分钟。

推送通知跟NSNotification的区别:

  (1)NSNotification是抽象的,不可见的

  (2)推送通知是可见的(能用肉眼看到)

iOS中提供了2种推送通知

  (1)本地推送通知(Local Notification)

  (2)远程推送通知(Remote Notification)//Remote远程的

推送通知的作用:可以让不在前台运行的app,告知用户app内部发生了什么事情

本地通知

本地通知是由本地应用触发的,它是基于时间行为的一种通知形式。不需要联网就能发出的推送通知(不需要服务器的支持)。

常用来定时提醒用户完成一些任务,比如:清理垃圾、记账、买衣服、看电影、玩游戏、吃药。

步骤:

  1. 创建UILocalNotification。 
  2. 设置处理通知的时间fireDate。 
  3. 配置通知的内容:通知主体、通知声音、图标数字等。 
  4. 配置通知传递的自定义数据参数userInfo(这一步可选)。 
  5. 调用通知,可以使用scheduleLocalNotification:按计划调度一个通知,也可以使用presentLocalNotificationNow立即调用通知。

 

  获得被调度(定制)的所有本地推送通知  (已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)

  @property(nonatomic,copy) NSArray *scheduledLocalNotifications;

    //调度本地通知   

  - (void)scheduleLocalNotification:(UILocalNotification *)notification;  

   立即发出本地推送通知,比如QQ收到消息,但是运行在前台使用没有意义。

  - (void)presentLocalNotificationNow:(UILocalNotification *)notification;

 

✨当用户点击本地推送通知,会自动打开app,这里有2种情况

1⃣️app并没有关闭,一直隐藏在后台

让app进入前台,并会调用AppDelegate的下面方法(并非重新启动app)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

 

2⃣️app已经被关闭(进程已死)

启动app,启动完毕会调用AppDelegate的下面方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey取出本地推送通知对象

 

AppDelegate.h文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   /*  UIUserNotificationTypeNone    = 0,      没有,没有本地通知

       UIUserNotificationTypeBadge   = 1 << 0, 接受图标右上角提醒数字

       UIUserNotificationTypeSound   = 1 << 1, 接受通知时候,可以发出音效

       UIUserNotificationTypeAlert   = 1 << 2, 接受提醒(横幅/弹窗)

      */

  //兼容iOS7.0及以后的版本, 版本适配    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];  //要使用本地通知,需要得到用户的许可
}        //被杀掉进程后,接收本地通知    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];    if (notification) {        //应用程序是点击本地通知启动的  可以在这里添加逻辑:添加新的控件、显示新的东西
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:notification.userInfo[@"notification"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];        [alert show];    }    return YES;}#pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行)//接收本地通知, 应用程序在后台-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{    NSLog(@"%@", notification.userInfo[@"notification"]);    //取消本地通知    [application cancelLocalNotification:notification];}#pragma mark 进入前台后设置消息信息- (void)applicationWillEnterForeground:(UIApplication *)application {    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];// 默认0 进入前台取消应用消息图标}

 

.m文件中

- (IBAction)addNotification:(id)sender {    //1.注册通知        //实例化通知对象    UILocalNotification *notification = [[UILocalNotification alloc] init];    //通知发出的时间, 当前时间7秒钟之后    notification.fireDate = [[NSDate date] dateByAddingTimeInterval:7];        //设置循环提醒    //循环标准的日历    notification.repeatCalendar = [NSCalendar currentCalendar];    //循环的标准  通知重复次数    notification.repeatInterval = NSCalendarUnitMinute;        //时区    notification.timeZone = [NSTimeZone defaultTimeZone];    //提醒的内容    notification.alertBody = @"今天没吃药,吃药时间到了。";    notification.alertAction = @"打开";  //待机界面的滑动动作提示    //提醒的声音    notification.soundName = @"msgcome.wav";        notification.alertLaunchImage=@"";//通过点击通知打开应用时的启动图片        //应用程序图标右上角显示的消息数    notification.applicationIconBadgeNumber = 1;    //附加的备注信息    notification.userInfo = @{
@"notification" : @"吃药"}; //2.调用通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }

 

使用通知的注意事项:

保证标记的内容是最新的

  • 对同一事件不要发出多个通知

  • 通知内容不用包含应用程序的名称

  • 对于标记型通知,当所有的事项都解决后,标记会自动消失

  • 在横幅和提醒中,以及顶部的通知中心里,iOS系统会自动在消息里显示应用程序的名称, 所以在设计通知的内容时,就无需包含app的名称了

  • 关注于信息的表达,而不是用户的动作。避免提示用户去点哪一个按钮或者是怎样打开app

  • 简短,最好不超过两行。长信息难以快速阅读,而且必然会有滚动条

  • 使⽤用句式大写(sentence-style capitalization,第一个单词的首字母大写)和合适的标点符号,结尾一般使用句号 

 

效果:

 

7秒之后:

点击后进入程序:

转载于:https://www.cnblogs.com/10-19-92/p/4882159.html

你可能感兴趣的文章
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
Sybase IQ导出文件的几种方式
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>