iOS10 消息推送

//1.appdelegate注册通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //注册通知
    if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
        //iOS10特有
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //必须些代理,不然无法监听通知的接受与点击
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if(granted){
                //允许点击
                NSLog(@"注册成功");
                [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                    NSLog(@"%@",settings);
                }];
            }else{
                //点击不允许
                NSLog(@"注册失败");
            }
        }];
    }else if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0){
        //ios8 - ios10
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil]];
    }else{
        //ios8 系统一下
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    //注册获得device Token
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    return YES;
}
//ios10 收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    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 前台手袋本地通知:%@",userInfo);
    }
    //执行这个方法,选择是否提醒用户有,badge、sound、alert、三种类型可以选择
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
    
}
//通知点击事件
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    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.title;//推送消息的标题
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]){
        NSLog(@"ios 收到远程通知:%@",userinfo);
    }else{
        //判断为本地通知
        NSLog(@"iOS10 收到本地通知:%@",userinfo);
    }
}
//2.创建通知
- (void) createUserNotification{
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
    content.title = @"推送测试通知";
    content.subtitle = @"通知测试";
    content.body = @"来时推送测试的通知消息";
    content.badge = @1;
    NSError *error = nil;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test.png" ofType:nil];
    //设置通知附件的内容
    UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
    if(error){
        NSLog(@"attachment error %@",error);
    }
    content.attachments = @[att];
    content.launchImageName = @"test";
    //设置声音
    UNNotificationSound *sound = [UNNotificationSound defaultSound];
    content.sound = sound;
    
    //触发模式
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    //设置UNNotificationRequest
    NSString *requestIdentifer = @"TestRequest";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger];
    //把通知驾到UNUserNotificationCenter 到指定触发点会被触发
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"add notification error:%@",error);
    }];
}

//参照链接

http://www.jianshu.com/p/f5337e8f336d

http://www.jianshu.com/p/3d602a60ca4f

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值