ios 10 新的notification UserNotif

作者: 等这姑娘老在我心里 | 来源:发表于2017-02-15 14:34 被阅读102次

    本篇文章不多写别的没有用的,因为在新闻和各大博客里面都有了,这里我就直接放上代码了
    详细介绍可以看猫爷的文章

    1.注册通知方式更改

    使用Usernotification之前 我们先需要引入frameworkUserNotifications.framework

    #import <UserNotifications/UserNotifications.h>
    //引入头文件 设置好delegate
    @interface AppDelegate ()<UNUserNotificationCenterDelegate>
    
    
    - (void)unUserNotificationSettting{
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (error == nil) {
                NSLog(@"注册通知成功");
            }else{
                NSLog(@"注册通知失败 error is %@",error);
            }
        }];
        
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            NSLog(@"unnotification setting is %@",settings);
        }];
    }
    
    

    2.发送通知

    - (void)sendUNUserNotification{
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
      //这里要用mutable content  要不然内容添加不进去 和dictionary原理一样
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
        content.title = @"titile";
        content.subtitle = @"subTitle";
        content.body = @"This is body";
        content.sound = [UNNotificationSound defaultSound];
        content.userInfo = @{@"url":@"https:www.baidu.com"};
    //新的通知可以加入图片,视频,声音等资源  方式是在content中加入attchments
        NSError *err = nil;
    //url 指的就是资源的URL 只可以是本地的 但是可以通过先下载到本地然后再使用  
    //本地图片
        NSString *localImageString = [[NSBundle mainBundle]pathForResource:@"image" ofType:@"gif"];
        NSURL *url = [NSURL fileURLWithPath:localImageString];
    //但是注意 不在bundle中的文件 通知发送出去后会自动删除这个文件,也就是说下载好的文件附在通知中  发送出去就没有了
        UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"notificaitonIden" URL:url options:nil error:&err];
        content.attachments = @[attachment];
    //新的通知可以替换前一条通知  方式就在于相同的request iden  不同的iden 就是多条通知
    //可以延迟发送  创建以个trigger 这里没有延迟 trigger 就写成nil
        //UNNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: [NSString stringWithFormat:@"usernotification%@",poi.name] content:content trigger:nil];
    
    //添加到发送队列中
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error == nil) {
                NSLog(@"发送本地通知成功");
            }else{
                NSLog(@"发送本地通知失败 error is %@",error);
            }
        }];
    
    
    

    3.响应通知 注意 不在原来的didreceive delegate中

    #pragma mark -  UNUserNotificationCenterDelegate
    //弹出通知之前  可以进行修改notification中的内容
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
        NSString *identifier = notification.request.identifier;
        UNNotificationPresentationOptions options = UNNotificationPresentationOptionNone; //默认什么也不做,不显示
        //项目中出现过的通知都设置为前台可以显示
        // options = UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert;
        if (identifier == nil) {
            completionHandler(options);
            return;
        }
        //设置完成之后必须调用这个回调,
        completionHandler(options);
    }
    //通知响应
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
        NSDictionary *dic = response.notification.request.content.userInfo;
        completionHandler();
    }
    

    相关文章

      网友评论

        本文标题:ios 10 新的notification UserNotif

        本文链接:https://www.haomeiwen.com/subject/fwlqwttx.html