• 推送通知(一)本地通知


    推送通知跟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秒之后:

    点击后进入程序:

  • 相关阅读:
    CV方向的高效阅读英文文献方法总结
    数据增强方法总结
    CNN结构演变总结(三)设计原则
    CNN结构演变总结(二)轻量化模型
    CNN结构演变总结(一)经典模型
    CNN可视化技术总结(四)--可视化工具与项目
    Codeforces972 D. Kuro and GCD and XOR and SUM 01Trie
    Codeforces 982 D. Shark
    Codeforces Round #700 (Div. 2) A~D题解
    codeforces 1004 D. Sonya and Matrix 构造
  • 原文地址:https://www.cnblogs.com/10-19-92/p/4882159.html
Copyright © 2020-2023  润新知