本地推送通知在iOS8上的不同

iOS8的不同点

你如果把上面的程序运行在iOS8上,会爆出如下错误

预习01-本地推送通知[掌握][615:7847] Attempting to schedule a local notification {fire date = Monday, July 13, 2015 at 9:02:25 AM China Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, July 13, 2015 at 9:02:25 AM China Standard Time, user info = { pageKey = friend; }} with an alert but haven‘t received permission from the user to display alerts

也就是在iOS8上要发送本地通知需要 请求用户权限 如何请求用户权限呢?一般在新版有变化的地方,在头文件中都会有相应的说明,所以点击到scheduleLocalNotification:方法中,看看有没有我们需要信息

点击进去,我们看到

意思就是说:在iOS8.0以后,在调度通知之前你需要使用UIApplication的对象方法registerUseNotificationSetting:来请求用户授权.

这种请求权限的代码一般放在didFinishLaunchingWithOptions:方法中,在用户不卸载的情况下,只需要请求一次,下次在运行就不用请求了!


//  1.如果是iOS8请求用户权限
    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {

        /*
         UIUserNotificationType:

         UIUserNotificationTypeBadge   = 1 << 0, // 接收到通知可更改程序的应用图标
         UIUserNotificationTypeSound   = 1 << 1, // 接收到通知可播放声音
         UIUserNotificationTypeAlert   = 1 << 2, // 接收到通知课提示内容
         如果你需要使用多个类型,可以使用 "|" 来连接
         */

//      向用户请求通知权限
//      categories暂时传入nil
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];

        [application registerUserNotificationSettings:setting];
    }

运行程序

测试点击通知,进入应用,也没问题



接下来,我们说说-[UIUserNotificationSettings settingsForTypes:categories:] 中的 categories

  • categories可以让我们发送通知之前预定义一些通知也就是通知上可以显示按钮,他需要是一个装有UIUserNotificationCategory类的对象的NSSet的对象. 但是官方推荐我们使用它的子类UIMutableUserNotificationCategory,来动态的添加通知的行为按钮,iOS8支持前台和后台的两种行为.
  • 通知Action按钮以长条展示如图 
  • 通知Action按钮以AlertView展示如图 
  • 注册分类,并在分类中添加不同的行为 由于注册用户通知设置代码量比较大我们实现一个新的方法registerUserNotification
- (void) registerUserNotification
{
    //      向用户请求通知权限
    /*
     UIUserNotificationType:用户通知的类型

     UIUserNotificationTypeBadge   = 1 << 0, // 接收到通知可更改程序的应用图标
     UIUserNotificationTypeSound   = 1 << 1, // 接收到通知可播放声音
     UIUserNotificationTypeAlert   = 1 << 2, // 接收到通知课提示内容
     如果你需要使用多个类型,可以使用 "|" 来连接
     */
//  1.设置用户通知权限类型
    UIUserNotificationType types = UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert;

//  2.创建通知的行为按钮

//  2.1创建第一个行为
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
//  2.1.1 设置行为的唯一标示
    action1.identifier = UIMutableUserNotificationActionBackground;
//  2.1.2 设置通知按钮的的标题
    action1.title = @"后台";
//      以什么样模式运行应用
//        UIUserNotificationActivationModeForeground, // 当应用在前台的时候触发
//        UIUserNotificationActivationModeBackground  // 即使应用不在前台也触发
    action1.activationMode = UIUserNotificationActivationModeBackground;
//  2.1.3 是否只有锁屏的锁屏状态下才能显示
    action1.authenticationRequired = NO;
//  2.1.4 按钮的性质
    action1.destructive = NO;

//  2.1创建第一个行为
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
//  2.1.1 设置行为的唯一标示
    action2.identifier =UIMutableUserNotificationActionForeground;
//  2.1.2 设置通知按钮的的标题
    action2.title = @"前台";
//      以什么样模式运行应用
    //        UIUserNotificationActivationModeForeground, // 当应用在前台的时候触发
    //        UIUserNotificationActivationModeBackground  // 即使应用不在前台也触发
    action2.activationMode = UIUserNotificationActivationModeForeground;
    //  2.1.3 用户必须输入密码才能执行
    action2.authenticationRequired = YES;
    //  2.1.4 按钮的性质(没有效果)
    action2.destructive = YES;

//  3.创建用户通知分类
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory  alloc]  init];
//  3.1 设置类别的唯一标识
    category.identifier = @"myCategory";
//  3.2 设置通知的按钮
    //    Context:
    //        UIUserNotificationActionContextDefault,  //默认上下文(情景)下的英文(通常都是)
    //        UIUserNotificationActionContextMinimal   //通知内容区域受限情况下内容
    [category   setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];

//  4.创建用户通知的设置信息
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithObject:category]];

//  注册设置
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
  • 在发送本地推送通知时候指定通知的分类标示
   //  9.设置通知的类别
    ln.category = @"myCategory";
  • 监听点击通知按钮的行为,在AppDelegate中实现监听通知按钮点击方法
/**
 *  当用户点击通知上定制的按钮执行的行为(注意:不点击行为按钮,不会进入该方法)
 *
 *  @param application       应用
 *  @param identifier        行为标识符
 *  @param notification      本地通知
 *  @param completionHandler 完成回调
 */
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
//  处理不同行为
    if ([identifier isEqualToString:UIMutableUserNotificationActionBackground]) {
        NSLog(@"后台运行程序");
    }else if ([identifier isEqualToString:UIMutableUserNotificationActionForeground]){
        NSLog(@"前台运行程序");
    }else{
        NSLog(@"其他");
    }
    /**
       You should call the completion handler as soon as you‘ve finished handling the action.
       当任务处理完毕时候,你应该尽快的调用completion的block.
     */

// 在当任务完成的时候,调用任务完成的block
    completionHandler();
}
时间: 2024-08-03 21:18:53

本地推送通知在iOS8上的不同的相关文章

本地推送通知小demo

本地推送通知: #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 在iOS8之后,Apple对用户隐私要求更加严格,所

本地推送通知界面跳转demo

本地推送通知界面跳转demo: /* 1.在发送本地通知的时候,通过userInfo属性来指示跳转到那个界面 2.监听本地通知的接收 1.当收到本地通知就会调用该代理方法 调用场景 1.如果应用程序在后台,当点击通知的时候 2.如果应用程序在前台,一旦收到本地通知,就会调用该方法 所以:判断如果应用程序在前台时候,不要执行界面跳转,来提高用户体验 3.如果应用程序被杀死了,那么这个方法就不再执行了 //- (void)application:(UIApplication *)applicatio

如何发送本地推送通知

如何发送本地推送通知 推送通知也属于UI的一部分,所以推送通知对象是以UI开头 方法送通知的代码方法控制器的-touchesBegan: withEvent: 中测试,比较合适,放到viewDidLoad方法,用户的注册请求还没有完成方法就调用了 创建本地通知 // 创建本地通知对象 UILocalNotification *ln = [[UILocalNotification alloc] init]; 设置本地通知属性(推荐一个一个属性测试运行) // 1.设置通知的内容(如果此属性不设置是

Swift 本地推送通知UILocalNotification

Notification是智能手机应用开发中常用的信息传递机制,它不用消耗更多资源去不停的检查信息状态,可以非常好的节省资源. 在iOS中分为两种通知:本地.远程.本地的UILocalNotification由全局的NotificationManager统一管理,我们只需要将本地通知对象添加到系统的Notification队列中就可以了,系统会在指定的时间激发本地通知. 本地推送通知:UILocalNotification 如果要使用推送通知,必须先在苹果的推送通知服务里注册你要使用哪几种类型的

本地推送通知UILocalNotification

1 - (IBAction)schedule { 2 // 1.创建本地推送通知对象 3 UILocalNotification *ln = [[UILocalNotification alloc] init]; 4 5 // 2.设置通知属性 6 // 音效文件名 7 ln.soundName = @"buyao.wav"; 8 9 // 通知的具体内容 10 ln.alertBody = @"重大新闻:xxxx xxxx被调查了...."; 11 12 // 锁

ios如何实现本地推送,兼容ios8

如果要兼容IOS8在IOS中实现本地推送,关键是要注意:ios8在实现本地推送时需要通过如下语句进行注册. [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; 至于IOS8之前版本的做法就不多说了,直接上代码.新建oc类文件(NotificationHelper),在NotificationHelper.h中声明相关方法如下: #import <UIKit/UIKit.h> @inte

(七十三)iOS本地推送通知的实现

iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被杀掉后是无法执行的,这时候就要借助远程通知,通过苹果的服务器转发通知到手机,本文只介绍本地通知的用法. ①对于iOS8及以上的版本,需要注册本地通知才能使用,一般在AppDelegate中注册: if ([[UIDevice currentDevice].systemVersion doubleVa

ios开发之-本地推送通知

不多说直接上代码 #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after

本地推送(通知)

// // AppDelegate.m // LocalNotification // // Created by xiaoyao on 15/3/17. // Copyright (c) 2015年 lijien. All rights reserved. // #import "AppDelegate.h" #import "LocalNotificationController.h" @interface AppDelegate () @end @implem