iOS 本地推送通知

1.什么是本地推送通知

  不需要联网的情况下,应用程序经由系统发出的通知

2.本地推送的使用场景

  定时提醒,如玩游戏、记账、闹钟、备忘录等

3.实现本地推送通知的步骤

  1. 创建本地推送通知的对象UILocalNotification
  2. 设置本地推送通知对象的属性
    • fireDate                                推送的时间
    • alertBody                              通知的内容
    • alertName                             锁屏时的标题
    • soundName                           音效名称
    • applicationIconBadgeNumber  徽章显示的数字
    • timeZone                               时区
    • 等等

  3. 将通知排入到应用程序中

4.点击通知内容的处理

  1. 应用没有关闭,在后台
    1. 自动进入前台
    2. 自动调用AppDelegate下的didReceiveLocalNotification方法
  2. 应用已关闭
    1. 自动进入应用
    2. 自动执行AppDelegate下的didFinishLaunchingWithOptions方法

5.代码

  1. 写在AppDelegate.m中
    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          
          if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {//iOS8以后需要询问用户是否允许接收通知
              //一下代码回实现的效果是
              //第一次运行程序,系统弹出一个提示框
              //询问用户是否允许接收通知
              UIUserNotificationType noteType = UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
              UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:noteType categories:nil];
              [application registerUserNotificationSettings:setting];
          }
          
          //如果是因为点击查看了通知而启动了应用程序
          //那么通知的信息都会存在launchOptions参数中
          UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
          if (notification != nil) {//点通知进来的
              UILabel *label = [[UILabel alloc]init];
              label.frame = CGRectMake(0, 40, 300, 200);
              label.backgroundColor = [UIColor blueColor];
              label.numberOfLines = 0;
              label.font =[UIFont systemFontOfSize:30];
              label.textColor = [UIColor whiteColor];
              label.text = [NSString stringWithFormat:@"3333333%@",notification.userInfo];
              [self.window.rootViewController.view addSubview:label];
              [application setApplicationIconBadgeNumber:0];
          }
          return YES;
      }
      
      
      /*
       1.App在前台,通知到了,直接自动执行该方法
       2.App在后台,通知到了,点击查看通知,该方法才执行
       3.App已经退出,通知到了,点击查看通知,此方法不执行,但是didFinishLaunchingWithOptions方法一定会被执行,通知传入的参数也可以在launching方法中获取到
       */
      - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
          UILabel *label = [[UILabel alloc]init];
          label.frame = CGRectMake(0, 250, 300, 200);
          label.backgroundColor = [UIColor grayColor];
          label.numberOfLines = 0;
          label.font =[UIFont systemFontOfSize:30];
          label.textColor = [UIColor whiteColor];
          //alertBody用于存储显示的通知的文字内容
          //uesrInfo用于存储额外要传递的通知内容
          label.text = [NSString stringWithFormat:@"%@",notification.userInfo];
          [self.window.rootViewController.view addSubview:label];
          //去掉应用程序图标中出现的红色数字提醒
          [application setApplicationIconBadgeNumber:0];
      }
  2. 写在注册通知的位置
    1. //1.创建本地通知对象
          UILocalNotification *notification = [[UILocalNotification alloc] init];
          
          //2.设置通知的一些属性
          notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];//10秒后发通知
          notification.alertBody = @"这是一条新的通知";
          notification.userInfo = @{@"name":@"张三",
                                     @"age":@20
                                    };
          notification.applicationIconBadgeNumber = 2;
          
          //3.将通知添加到应用程序的日程清单中
          UIApplication *application = [UIApplication sharedApplication];
          [application scheduleLocalNotification:notification];

demo:https://github.com/TigerCui/LocalNotification.git

原文地址:https://www.cnblogs.com/czq1989/p/5319656.html