iOS本地推送

import

UNUserNotificationCenter *notiCenter = [UNUserNotificationCenter currentNotificationCenter];
notiCenter.delegate = self;
//用户授权
[notiCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"注册成功");
dispatch_async(dispatch_get_main_queue(), ^{
[application registerForRemoteNotifications];
});
}
}];
//获取当前通知设置
[notiCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

}];

// iOS 10收到通知 代理方法

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)) {
    NSDictionary * userInfo = notification.request.content.userInfo;
    UNNotificationRequest *request = notification.request; // 收到推送的请求
    UNNotificationContent *content = request.content; // 收到推送的消息内容
    NSNumber *badge = content.badge; // 推送消息的角标
    NSString *body = content.body; // 推送消息体

    UNNotificationSound *sound = content.sound; // 推送消息的声音
    NSString *subtitle = content.subtitle; // 推送消息的副标题
    NSString *title = content.title; // 推送消息的标题

    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    NSLog(@"iOS10 前台收到远程通知:%@", userInfo);
    } else {
    // 判断为本地通知
    NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
    }
    if ([userInfo[@"action"]integerValue] == 1) {
    completionHandler(UNNotificationPresentationOptionSound); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
    } else {
    completionHandler(UNNotificationPresentationOptionSound|
    UNNotificationPresentationOptionAlert|
    UNNotificationPresentationOptionBadge); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
    }
    }

// 通知的点击事件

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(ios(10.0)) {

    NSDictionary * userInfo = response.notification.request.content.userInfo;
    UNNotificationRequest *request = response.notification.request; // 收到推送的请求
    UNNotificationContent *content = request.content; // 收到推送的消息内容
    NSNumber *badge = content.badge; // 推送消息的角标
    NSString *body = content.body; // 推送消息体
    UNNotificationSound *sound = content.sound; // 推送消息的声音
    NSString *subtitle = content.subtitle; // 推送消息的副标题
    NSString *title = content.title; // 推送消息的标题
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    NSLog(@"iOS10 收到远程通知:%@", userInfo);
    } else {
    // 判断为本地通知
    NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
    }
    completionHandler(); // 系统要求执行这个方法
    }

//实现本地通知方法调用

  • (void)localMessagePushNotification:(TMMessageModel *)model {

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    // 声音
    // 默认声音
    content.sound = [UNNotificationSound defaultSound];
    // 添加自定义声音
    // content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];
    // 角标 (我这里测试的角标无效,暂时没找到原因)
    content.badge = @1;
    content.body = @"";
    content.userInfo = @{@"action":@"1"};
    // 多少秒后发送,可以将固定的日期转化为时间
    // NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:0.] timeIntervalSinceNow];
    // NSTimeInterval time = 10;
    // repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];

    /*
    //如果想重复可以使用这个,按日期
    // 周一早上 8:00 上班
    NSDateComponents *components = [[NSDateComponents alloc] init];
    // 注意,weekday默认是从周日开始
    components.weekday = 2;
    components.hour = 8;
    UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
    */
    // 添加通知的标识符,可以用于移除,更新等操作
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:model.messageId content:content trigger:trigger];

    [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
    NSLog(@"成功添加推送");
    }];
    }

你可能感兴趣的:(iOS本地推送)