iOS10本地推送

iOS10對推送做了比較大的變更游盲,主要有以下4點

  • 1.推送的內(nèi)容更加豐富到逊,由之前的alert到現(xiàn)在的title subtitle body attachment
  • 2.本地和遠程推送全部是由trigger觸發(fā)(更加面向?qū)ο螅?/li>
  • 3.可以為推送增加附件 如圖片兼耀,音頻应又,視頻等
  • 4.可以方便的更新推送的內(nèi)容

下面來以本地推送為例鸯乃,

1.在iOS10中首先要獲取權(quán)限

#import <UserNotifications/UserNotifications.h>
遵守UNUserNotificationCenterDelegate協(xié)議

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions: UNAuthorizationOptionBadge|UNAuthorizationOptionSound |UNAuthorizationOptionAlert  completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"注冊通知成功");
        }else{
            NSLog(@"注冊通知失敗");
        }
    }];
    
    return YES;
}

此時運行的話會出現(xiàn)提示框選擇allow

Paste_Image.png

2.設(shè)置本地推送的內(nèi)容

- (void)creatLocalUserNotification{
    UNTimeIntervalNotificationTrigger *trigger =[ UNTimeIntervalNotificationTrigger triggerWithTimeInterval:8 repeats:NO];
    
    //創(chuàng)建通知的內(nèi)容
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];
    content.title = [NSString stringWithFormat:@"當前時間提醒 %@",[NSDate date]];
    content.subtitle = [NSString stringWithFormat:@"超模VS網(wǎng)紅模特--subtitle"];
    content.body = @"我愛超模全國十強誕生夜--body";
    content.badge = @1;
    content.sound = [UNNotificationSound defaultSound];
    content.categoryIdentifier = @"category";
    NSString *path =  [[NSBundle mainBundle]pathForResource:@"222" ofType:@"png"];
    NSError *error = nil;
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment " URL:[NSURL fileURLWithPath:path] options:nil error:&error];
    if (error) {
        NSLog(@"error: %@",error);
    }
    content.attachments = @[attachment];  
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"request" content:content trigger:trigger];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"推送已經(jīng)成功 ");
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"推送成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:nil];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alert addAction:action];
            [alert addAction:action2];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
    
}

<<< 設(shè)置好了運行下剖张,如下圖所示


Paste_Image.png

<<< 把通知往下拖拽下派阱,如圖所示可以看到attachment


Paste_Image.png

3.額外補充

以上兩點就可以設(shè)置一個簡單的本地推送诬留,當我們有其他額外的要求的時候,比如直接在推送的通知上面編輯以及程序運行在前臺的時候要不要顯示推送等贫母,那么該如果做呢文兑?
給UNNotificationCategory添加action 注意不同的options代表用戶點擊之后進行不同的操作


#import <Foundation/Foundation.h>
#import <UserNotifications/UserNotifications.h>

@interface NotificationAction : NSObject

+(void)addNotificationAction;

@end
#import "NotificationAction.h"

@implementation NotificationAction
+(void)addNotificationAction{
    UNNotificationAction *look = [UNNotificationAction actionWithIdentifier:@"action.look" title:@"查看詳情" options:UNNotificationActionOptionForeground];//點擊action打開APP
    UNNotificationAction *join = [UNNotificationAction actionWithIdentifier:@"action.join" title:@"我要參加" options:UNNotificationActionOptionAuthenticationRequired];//點擊action需要解鎖
    UNNotificationAction *cancle = [UNNotificationAction actionWithIdentifier:@"action.cancle" title:@"我想靜靜" options:UNNotificationActionOptionDestructive];//顯示為紅色
    UNTextInputNotificationAction *input = [UNTextInputNotificationAction actionWithIdentifier:@"action.input" title:@"輸入" options:UNNotificationActionOptionForeground textInputButtonTitle:@"發(fā)送" textInputPlaceholder:@"Placeholder"];
    
    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[look,join,cancle,input] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center setNotificationCategories:[NSSet setWithObject:category]];
}
@end

然后在creatLocalUserNotification或者AppDelegate里面調(diào)用這個方法,此時的通知是下面這樣的

Paste_Image.png

上面的這些設(shè)置腺劣,當app在前臺運行的時候當收到推送的時候就不會在屏幕上方顯示了绿贞,那么是否可以讓在前臺的時候也展示推送的呢?
此時需要用到UNUserNotificationCenterDelegate在下面的方法中設(shè)置一下就可以了

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound);
}
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末誓酒,一起剝皮案震驚了整個濱河市樟蠕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌靠柑,老刑警劉巖寨辩,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異歼冰,居然都是意外死亡靡狞,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門隔嫡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甸怕,“玉大人甘穿,你說我怎么就攤上這事∩液迹” “怎么了温兼?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長武契。 經(jīng)常有香客問我募判,道長,這世上最難降的妖魔是什么咒唆? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任届垫,我火速辦了婚禮,結(jié)果婚禮上全释,老公的妹妹穿的比我還像新娘装处。我一直安慰自己,他們只是感情好浸船,可當我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布妄迁。 她就那樣靜靜地躺著,像睡著了一般糟袁。 火紅的嫁衣襯著肌膚如雪判族。 梳的紋絲不亂的頭發(fā)上躺盛,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天项戴,我揣著相機與錄音,去河邊找鬼槽惫。 笑死周叮,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的界斜。 我是一名探鬼主播仿耽,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼各薇!你這毒婦竟也來了项贺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤峭判,失蹤者是張志新(化名)和其女友劉穎开缎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體林螃,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡奕删,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了疗认。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片完残。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡伏钠,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谨设,到底是詐尸還是另有隱情熟掂,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布扎拣,位于F島的核電站打掘,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鹏秋。R本人自食惡果不足惜尊蚁,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望侣夷。 院中可真熱鬧横朋,春花似錦、人聲如沸百拓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽衙传。三九已至决帖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蓖捶,已是汗流浹背地回。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留俊鱼,地道東北人刻像。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像并闲,于是被迫代替她去往敵國和親细睡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容