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
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è)置好了運行下剖张,如下圖所示
<<< 把通知往下拖拽下派阱,如圖所示可以看到attachment
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)用這個方法,此時的通知是下面這樣的
上面的這些設(shè)置腺劣,當app在前臺運行的時候當收到推送的時候就不會在屏幕上方顯示了绿贞,那么是否可以讓在前臺的時候也展示推送的呢?
此時需要用到UNUserNotificationCenterDelegate
在下面的方法中設(shè)置一下就可以了
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound);
}