iOS10本地推送

iOS10對推送做了比較大的變更,主要有以下4點(diǎn)

  • 1.推送的內(nèi)容更加豐富,由之前的alert到現(xiàn)在的title subtitle body attachment
  • 2.本地和遠(yuǎn)程推送全部是由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;
}

此時(shí)運(yùn)行的話會(huì)出現(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:@"當(dāng)前時(shí)間提醒 %@",[NSDate date]];
    content.subtitle = [NSString stringWithFormat:@"超模VS網(wǎng)紅模特--subtitle"];
    content.body = @"我愛超模全國十強(qiáng)誕生夜--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è)置好了運(yùn)行下,如下圖所示


Paste_Image.png

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


Paste_Image.png

3.額外補(bǔ)充

以上兩點(diǎn)就可以設(shè)置一個(gè)簡單的本地推送,當(dāng)我們有其他額外的要求的時(shí)候,比如直接在推送的通知上面編輯以及程序運(yùn)行在前臺(tái)的時(shí)候要不要顯示推送等,那么該如果做呢?
給UNNotificationCategory添加action 注意不同的options代表用戶點(diǎn)擊之后進(jìn)行不同的操作


#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];//點(diǎn)擊action打開APP
    UNNotificationAction *join = [UNNotificationAction actionWithIdentifier:@"action.join" title:@"我要參加" options:UNNotificationActionOptionAuthenticationRequired];//點(diǎn)擊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)用這個(gè)方法,此時(shí)的通知是下面這樣的

Paste_Image.png

上面的這些設(shè)置,當(dāng)app在前臺(tái)運(yùn)行的時(shí)候當(dāng)收到推送的時(shí)候就不會(huì)在屏幕上方顯示了,那么是否可以讓在前臺(tái)的時(shí)候也展示推送的呢?
此時(shí)需要用到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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容