美文网首页
iOS10 简单的本地通知

iOS10 简单的本地通知

作者: __Seven | 来源:发表于2017-05-23 14:51 被阅读95次

    说好的写这个关于关于iOS 10 的简单本地通知,其实开始是我自己看文档看复杂了,其实就是一个很简单的逻辑,只是和iOS8之前的通知有了点变化,iOS8用的是这样的本地通知
    直接上代码

           UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"action1_identifier";
        action1.title=@"Accept";
        action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2_identifier";
        action2.title=@"Reject";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
        action2.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
        action2.destructive = YES;
        
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"category1";//这组动作的唯一标示
        [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
        
        UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
                                                                                     categories:[NSSet setWithObject:categorys]];
    

    在iOS10之后这样的推送通知更加的人性化,更方便的处理了:是这样的

       NSString * identife = @"TestRequest";
       UNUserNotificationCenter *notificationCenter   =[UNUserNotificationCenter currentNotificationCenter];
        notificationCenter.delegate = self;
       [notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted&&!error) {
            
                UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc]init];
                content.badge = @2;
                content.title = @"seven的通知";
                content.subtitle = @"qgutech测试";
                content.body = @"通知来了";
                content.sound = [UNNotificationSound defaultSound];
                content.categoryIdentifier = @"testsend";
                UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO];
                UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:identife content:content trigger:trigger];
              [notificationCenter addNotificationRequest:notificationRequest
              withCompletionHandler:^(NSError * _Nullable error) {
                if (!error) {
                     NSLog(@"sussce");
                }
            }];
                
            }else{
                NSLog(@"fail");
            }
        }];
    

    主要iOS10的通知要注意点的是这个类UNMutableNotificationContent,这是主要描述通知内容的,主要有这些属性:

       //角标
        NSNumber *badge;
       //标题
        NSString *title;
       //子标题
        NSString *subtitle;
      //内容体
        NSString *body;
       //声音
       UNNotificationSound *sound ;
       //线程标识
       NSString *threadIdentifier;
      //推送附件
       NSArray <UNNotificationAttachment *> *attachments;
    

    而且这个iOS10 的推送可以自定义的推送内容,和展示。
    还有就是启动这个通知的方式:
    可以设置时间定时启动可以循环

       UN TimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO];
    

    还可以设置是地理位置的发送

       UNLocationNotificationTrigger (本地通知)地理位置的一种通知,
    

    当用户进入或离开一个地理区域来通知。

    指定的日期

          UNCalendarNotificationTrigger(本地通知) 一定日期之后
    

    最后只要加入这个通知就可以了

           UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:identife content:content trigger:trigger];
              [notificationCenter addNotificationRequest:notificationRequest
              withCompletionHandler:^(NSError * _Nullable error) {
                if (!error) {
                     NSLog(@"sussce");
                }
            }];
                
            }else{
                NSLog(@"fail");
            }
        }];
    

    这就是这次的简单本地通知的记录。还请看到的多多指教。

    相关文章

      网友评论

          本文标题:iOS10 简单的本地通知

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