关于IOS 的地面文告

方法中对image键值路径请求数据下载到本地,或者做其他处理变动,                //点击不允许                NSLog(@”注册通知失败”),registerForRemoteNotifications,iOS10 的推送通知支持图片啦,通知支持的样式,//设置completionHandler的UNNotificationPresentationOptions可以使App在前台活跃状态下依然弹出通知消息,&lt

金沙澳门官网7817网址 2
  1. UserNotifications官方文书档案说明
  2. 剧情丰硕;能够博得客商是还是不是允许推送等notification
    setting新闻;提供trigger;app在前台能够捕捉并管理将要触发的推送
  3. 框架是UserNotifications.framework;辅助UserNotificationsUI.framework自定义公告的UI展现

iOS10 本地通告

    #import@interfaceAppDelegate() @property(nonatomic) NSInteger
badge;@end- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([[UIDevice currentDevice] systemVersion].floatValue >=
10.0) {

        UNUserNotificationCenter *center = [UNUserNotificationCenter
currentNotificationCenter];

        center.delegate = self;

        [center
requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |
UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {

            if (granted) {

                //点击允许                NSLog(@”注册通告成功”);

                [center
getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *
_Nonnull settings) {

                    [self registerNotification:1];

                }];

            } else {

                //点击不允许                NSLog(@”注册公告未果”);

            }

        }];

        //注册推送(同iOS8)        [[UIApplication
sharedApplication] registerForRemoteNotifications];

    }

    else if([[UIDevice currentDevice] systemVersion].floatValue <
10.0 &&[[UIDevice currentDevice] systemVersion].floatValue >=
8.0){//iOS8到iOS10       
//1.创设音信上边要丰盛的动作(开关的花样显得出来)       
UIMutableUserNotificationAction *action =
[[UIMutableUserNotificationAction alloc] init];

        action.identifier = @”action”;//按键的标记       
action.title=@”Accept”;//按键的标题        action.activationMode =
UIUserNotificationActivationModeForeground;//当点击的时候运转程序       
//    action.authenticationRequired = YES;        //   
action.destructive = YES;        UIMutableUserNotificationAction
*action2 = [[UIMutableUserNotificationAction alloc] init];

        action2.identifier = @”action2″;

        action2.title=@”Reject”;

        action2.activationMode =
UIUserNotificationActivationModeBackground;//当点击的时候不运营程序,在后台管理 
      action.authenticationRequired =
YES;//供给解锁技艺处理,借使action.activationMode =
UIUserNotificationActivationModeForeground;则那本本性被忽略;       
action.destructive = YES;

        //2.创造动作(开关)的门类集结       
UIMutableUserNotificationCategory *categorys =
[[UIMutableUserNotificationCategory alloc] init];

        categorys.identifier =
@”alert”;//那组动作的有一无二标示,推送公告的时候也是依照那个来区分       
[categorys setActions:@[action,action2]
forContext:(UIUserNotificationActionContextMinimal)];

        //3.创建UIUserNotificationSettings,并安装消息的来得类项目     
  if ([[UIApplication sharedApplication]
respondsToSelector:@selector(registerUserNotificationSettings:)]) {

            UIUserNotificationType type =  UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

            UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:type categories:nil];

            [[UIApplication sharedApplication]
registerUserNotificationSettings:settings];

        }

    }

    else {//iOS8以下        [金沙澳门官网7817网址,application
registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];

    }

    return YES;

}

发送通告:

-(void)registerNotification:(NSInteger)alerTime

{

    //
1、成立文告内容,注:这里得用可变类型的UNMutableNotificationContent,不然内容的性质是只读的 
  UNMutableNotificationContent *content =
[[UNMutableNotificationContent alloc] init];

    // 标题    content.title = @”那是打招呼”;

    // 次标题    content.subtitle = @”这是通报subtitle”;

    // 内容    content.body =
@”那是通报body那是打招呼body那是打招呼body那是打招呼body这是通报body那是通报body那是通报body那是打招呼body那是打招呼body那是打招呼body那是通报body这是通报body那是通报body那是打招呼body”;

    self.badge++;

    // app显示通告数量的角标    content.badge = [NSNumber
numberWithInteger:self.badge];

    // 通告的唤起声音,这里用的暗中认可的声音    content.sound =
[UNNotificationSound defaultSound];

    NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@”1″
withExtension:@”png”];

    UNNotificationAttachment *attachment = [UNNotificationAttachment
attachmentWithIdentifier:@”imageIndetifier” URL:imageUrl options:nil
error:nil];

    // 附属类小部件 能够是音频、图片、录像 这里是一张图片    content.attachments
= @[attachment];

    // 标识符    content.categoryIdentifier = @”categoryIndentifier”;

    // 2、创制公告触发    /* 触发器分二种:

    UNTimeIntervalNotificationTrigger :
在一定期间后触发,假诺设置双重的话,timeInterval不可能小于60

    UNCalendarNotificationTrigger : 在某天某时触发,可再度

    UNLocationNotificationTrigger : 步入或离开某些地理区域时接触

    */    //UNTimeIntervalNotificationTrigger *trigger =
[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60
repeats:YES];    NSDateComponents *components = [NSDateComponents
new]; components.second = 2.0f;

    UNCalendarNotificationTrigger *trigger =
[UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:components repeats:YES];

    // 3、创造通告需要    UNNotificationRequest *notificationRequest =
[UNNotificationRequest requestWithIdentifier:@”KFGroupNotification”
content:content trigger:trigger];

    // 4、将供给参预公告中央    [[UNUserNotificationCenter
currentNotificationCenter] addNotificationRequest:notificationRequest
withCompletionHandler:^(NSError * _Nullable error) {

        if (error == nil) {

            NSLog(@”已成功加推送%@”,notificationRequest.identifier);

        }

    }];

}

iOS10 推送代理

#pragma mark – iOS10 推送代理

– (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void
(^)(UNNotificationPresentationOptions))completionHandler {

    //应用在前台收到通知    NSLog(@”========%@”, notification);

    //假若需求在动用在前台也展示布告   
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);

}// 对通报实行响应-
(void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler {

    // 依据项目的志符管理指标反应    if
([response.notification.request.content.categoryIdentifier
isEqualToString:@”categoryIndentifier”]) {

        [self handleResponse:response];

    }

    completionHandler();

}

– (void)handleResponse:(UNNotificationResponse *)response {

    NSString *actionIndentifier = response.actionIdentifier;

    NSLog(@”%@”,@”管理通报”);

}

iOS8- 10 推送代理

#pragma mark – iOS8 –  10 推送代理-
(void)application:(UIApplication*)application
handleActionWithIdentifier:(NSString*)identifier
forLocalNotification:(UILocalNotification*)notification
completionHandler:(void(^)())completionHandler{if([identifier
isEqualToString:@”XXX”]) {    }    completionHandler();}-
(void)application:(UIApplication*)application
handleActionWithIdentifier:(NSString*)identifier
forRemoteNotification:(NSDictionary*)userInfo
completionHandler:(void(^)())completionHandler{NSLog(@”handleActionWithIdentifier”);}//点击本地推送-
(void)application:(UIApplication*)application
didReceiveLocalNotification:(UILocalNotification*)notification{NSLog(@”didReceiveLocalNotification
notification”);}//点击远程推送-
(void)application:(UIApplication*)application
didReceiveRemoteNotification:(NSDictionary*)userInfo{NSLog(@”didReceiveRemoteNotification
userInfo”);}

转自 iOS10
本地通告

写的很鲜明

iOS10
新特征一出,各家纷纭进军研讨新特点能给场景智能化所拉动的好处,从widget到推送通告的开展。不奇异的,当小编还从今后得及跟公司调换新特征在软件上的运用时,就被公司拉来商量了,今马来西亚人先就iOS
10 在推送公告上的新添API以及采纳格局说一说:

UserNotification类介绍

  • UNNotificationCenter:文告管理宗旨,单例,公告的注册,接收文告后的回调管理等,是UserNotification框架的主题。

  • UNNotification:公告对象,当中封装了通报告请示求。

  • UNNotificationSettings:布告有关设置。

  • UNNotificationCategory:文告模板。

  • UNNotificationAction:用于定义通告模板中的客商交互行为。

  • UNNotificationRequest:注册通告须求,在那之中定义了通告的开始和结果和接触方式。

  • UNNotificationResponse:接收到通报后的回执。

  • UNNotificationContent:布告的具体内容。

  • UNNotificationTrigger:公告的触发器,由其子类具体定义。

  • UNNotificationAttachment:公告附属类小部件类,为布告内容增加媒体附属类小部件。

  • UNNotificationSound:定义文告音响效果。

  • UNPushNotificationTrigger:远程文告的触发器,UNNotificationTrigger子类。

  • UNTimeInervalNotificationTrigger:计时通报的触发器,UNNotificationTrigger子类。

  • UNCalendarNotificationTrigger:周期布告的触发器,UNNotificationTrigger子类。

  • UNLocationNotificationTrigger:地域公告的触发器,UNNotificationTrigger子类。

  • UNNotificationCenterDelegate:左券,当中措施用于监听公告状态。

2.1 本地公告的trigger

  1. 新职能trigger,能够在特定条件触发,有三类:UNTimeIntervalNotificationTrigger、UNCalendarNotificationTrigger、UNLocationNotificationTrigger
  2. UNTimeIntervalNotificationTrigger:一段时间后触发

//2min后提醒UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:120 repeats:NO];
  1. UNCalendarNotificationTrigger
    :调用triggerWithDateMatchingComponents:repeats:
    举办挂号;时间点音讯用 NSDateComponents

//每周日早上 8:00 提醒NSDateComponents *components = [[NSDateComponents alloc] init];components.weekday = 1;components.hour = 8;UNCalendarNotificationTrigger *trigger2 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

//NSDateComponets的注意点,有了时间并不能得到weekday NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.day = 11; dateComponents.month = 7; dateComponents.year = 2016; //输出结果是NSDateComponentUndefined = NSIntegerMax NSLog(@"%td", dateComponents.weekday); //根据calendar计算出当天是周几 NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDate *date = [gregorianCalendar dateFromComponents:dateComponents]; NSInteger weekday = [gregorianCalendar component:NSCalendarUnitWeekday fromDate:date]; NSLog(@"%td", weekday); dateComponents.weekday = weekday; NSLog(@"%@", dateComponents);
  1. UNLocationNotificationTrigger:调用triggerWithRegion:repeats:实行注册,地区消息运用CLRegion,能够布置region属性
    notifyOnEntry和notifyOnExit,是在步向地面、从地面出来可能两者都要的时候举行通报

//圆形区域,进入时候进行通知CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.335400, -122.009201);CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:center radius:2000.0 identifier:@"Headquarters"];region.notifyOnEntry = YES;region.notifyOnExit = NO;UNLocationNotificationTrigger* trigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:NO];

(不忧虑的说,程序媛一枚,接待点赞)

权限申请

权力申请代码

    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //设置代理
            [UNUserNotificationCenter currentNotificationCenter].delegate = self;
        }
    }];

requestAuthorizationWithOptions权力参数介绍

typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) {
    UNAuthorizationOptionBadge   = (1 << 0), //数字
    UNAuthorizationOptionSound   = (1 << 1), //声音
    UNAuthorizationOptionAlert   = (1 << 2), //弹框
    UNAuthorizationOptionCarPlay = (1 << 3), //车载设备
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

2.2 Content

UNNotificationContent:属性readOnlyUNMutableNotificationContent:属性有title、subtitle、body、badge、sound、lauchImageName、userInfo、attachments、categoryIdentifier、threadIdentifier

本地消息内容 内容限制大小 展示
title NSString 限制在一行,多出部分省略号
subtitle NSString 限制在一行,多出部分省略号
body NSString 通知栏出现时,限制在两行,多出部分省略号;预览时,全部展示

注意点: body中printf风格的转义字符,比方说要满含%,必要写成%%
才会展现,\同样

金沙澳门官网7817网址 1notification

  1. 地面推送和远程推送同期都可协助附带Media
    Attachments。可是远程通告要求贯彻布告服务增添UNNotificationServiceExtension,在service
    extension里面去下载attachment,可是需求小心,service
    extension会限制下载的小时,况且下载的文件大小也团体带头人期以来被界定。这里毕竟是一个推送,实际不是把具有的内容都推送给客户。所以你应当去推送一些压缩比例之后的本子。举例图片,推送里面附带缩略图,当客商张开app之后,再去下载完整的高清图。摄像就顺手录制的关键帧或然起首的几秒,当客商展开app之后再去下载完整摄像。
  2. attachment帮忙图片,音频,录像,附属类小部件帮助的类型及大小
  3. 系统会在文告注册前校验附属类小部件,假如附件出标题,公告注册退步;校验成功后,附属类小部件会转入attachment
    data store;倘使附属类小部件是在app bundle,则是会被copy来代替move
  4. media attachments能够选用3d touch实行预览和操作
  5. attachment data store的职位?利用代码测试获取在磁盘上的图样文件作为attachment,会意识注册完布告后,图片文件被移除,在app的沙盒中找不到该公文在哪儿;
    想要获取已存在的附属类小部件内容,文档中说起能够经过UNUserNotificationCenter中艺术,但最近文书档案中那2个措施照旧灰的

getDataForAttachment:withCompletionHandler: getReadFileHandleForAttachment:withCompletionHandler:

金沙澳门官网7817网址 2sound
notification

UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];content.title = [NSString localizedUserNotificationStringForKey:@"Hello 5 seconds!" arguments:nil];content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body" arguments:nil]; content.subtitle = [NSString localizedUserNotificationStringForKey:@"good day" arguments:nil];content.sound = [UNNotificationSound defaultSound];//NSURL *url = [[NSBundle mainBundle] URLForResource:@"image_1" withExtension:@"jpeg"];NSURL *url = [[NSBundle mainBundle] URLForResource:@"xiongben" withExtension:@"gif"];UNNotificationAttachment *attch = [UNNotificationAttachment attachmentWithIdentifier:@"photo" URL:url options:nil error:nil];content.attachments = @[attch];//......

iOS10 的推送布告协助图片啦!!协助录制了!!帮衬自定义样式了!!

开创公告

创办通告代码

    //创建通知
    //通知可变内容类
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.badge = @2;
    //通知内容
    content.body = @"This is iOS10 new Notification Body";
    //默认通知提示音
    content.sound = [UNNotificationSound defaultSound];
    //副标题 - 新特性
    content.subtitle = @"this is subTitle";
    //标题
    content.title = @"this is title";
    //启动图片 - 新特性
    content.launchImageName = @"launchImg";
    //设置延迟执行
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
    //创建Request
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"requestIdentifier" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"addNotificationCompletionHandler");
    }];