iOS10后进行本地推送的一个例子
in 技术日志 with 0 comment and 2851 read

iOS10后进行本地推送的一个例子

in 技术日志 and 2852 read

第一步,添加UserNotification

add binary library

在 Link Binary With Libraries 中添加 UserNotifications.framework。

第二步,设置推送委托

AppDelegate.m中,开头的类扩展部分按照下面的代码修改。

#import <UserNotifications/UserNotifications.h>
#import "AppDelegate.h"
@interface AppDelegate ()<UNUserNotificationCenterDelegate>// 注意这个协议
 
@end

然后在application didFinishLaunchingWithOptions:方法中注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // 使用 UNUserNotificationCenter 来管理通知
 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
 //监听回调事件
 center.delegate = self;
  
 //iOS 10 使用以下方法注册,才能得到授权
 [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
       completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Enable or disable features based on authorization.
        if (!error) {
            NSLog(@"request authorization succeeded!");
        }
       }];
  
 //获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
 [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
   
 }];
 return YES;
}

然后实现下面两个协议要求的回调方法。

#pragma mark - UNUserNotificationCenterDelegate
//在展示通知前进行处理,即有机会在展示通知前再修改通知内容。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
 //1. 处理通知
  
 //2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
 completionHandler(UNNotificationPresentationOptionAlert);
}

//点击推送消息后回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}

@end

第三步,在需要用到本地通知的地方,运用添加下面的代码。

注意,调用这个方法的时候,记得在开头写上下面的代码:

#import <UserNotifications/UserNotifications.h>

比如说现在我有个方法的参数是一个NSDate对象,就可以用这一句代码来将NSDate转换成TimeInterval:

NSInteger alerTime = [date timeIntervalSinceNow];

然后利用这个alerTime设置定时的推送:

//使用 UNNotification 本地通知
+(void)registerNotification:(NSInteger )alerTime{
  
 // 使用 UNUserNotificationCenter 来管理通知
 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
  
 //需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。
 UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
 content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
 content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
 arguments:nil];
 content.sound = [UNNotificationSound defaultSound];
  
 // 在 alertTime 后推送本地推送
 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
 triggerWithTimeInterval:alerTime repeats:NO];
 
 UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
 content:content trigger:trigger];
  
 //添加推送成功后的处理!
 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  [alert addAction:cancelAction];
  [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
 }];
}

注:封面图是《绝对绝望少女》的两位女主人公,苗木困和【数据删除】。因为武器是喇叭,所以和通知有一点微妙的联系。

Responses