免费开源的iOS开发学习平台

本地消息推送(基于iOS10 UserNotifications框架):5-代码示例

本节通过一个简单的示例,说明如何使用UserNotifications框架实现本地消息推送。

在推送中心注册推送请求

UNUserNotificationCenter类是一个管理推送请求的类,类似于NSNotificationCenter,所以我们需要把推送请求在推送中心进行注册。整个过程包括如下几个步骤:

  • 获取UNUserNotificationCenter实例center

  • 创建一个推送请求request,需要预先创建UNMutableNotificationContent对象以及UNTimeIntervalNotificationTrigger对象

  • 把推送请求request添加到推送中心center中

-(void)registerNotification{
    // 获取UNUserNotificationCenter实例center
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    
    //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"99iOS" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"九九学院"
                                                         arguments:nil];
    content.subtitle = @"苹果iOS进阶之路";
    content.sound = [UNNotificationSound defaultSound];
    content.launchImageName =@"99logo";
    
    // 在 alertTime 后推送本地推送
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:60 repeats:YES];
    //创建一个推送请求request
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"99iOSNotification"
                                                                          content:content
                                                                          trigger:trigger];
    //添加推送请求request到center
    [center addNotificationRequest:request withCompletionHandler:nil];  
}

其他相关设置

当一个App需要推送消息,推送中心center则需要预先获取消息推送功能的授权,通常可以在didFinishLaunchingWithOptions:方法中完成,另外,我们还需要设置推送中心center的代理,以便可以在代理方法中进行操作。

设置操作主要包括:

  • 推送中心center预先向用户申请,以获取消息推送功能的授权
  • 设置推送中心center的代理对象
  • 设置需要在center中注册的推送请求
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    //推送中心center预先获取消息推送功能的授权
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) { }];
    //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { }];
    //设置推送中心center的代理对象
    center.delegate = self;
    //设置需要在center中注册的推送请求
    [self registerNotification];
    return YES;
}

实现UNUserNotificationCenterDelegate代理方法

在UNUserNotificationCenterDelegate代理方法中,我们可以获取到推送到用户终端上的推送消息,基于推送消息中携带的信息,我们可以进行一些具体的后续操作。例如,在新闻类的App中,可以携带推送的新闻URL,我们可以让用户点击推送消息后,直接跳转到该条新闻的阅读界面。在下方的示例代码中,我们仅仅打印出推送消息中携带的信息。

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSLog(@"%s",__func__);
    UNNotificationContent * content = response.notification.request.content;
    NSLog(@"notification body:%@",content.body);
    NSLog(@"notification subtitle:%@",content.subtitle);
    NSLog(@"notification title:%@",content.title);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSLog(@"%s",__func__);
}

运行结果如下。