1. 程式人生 > >iOS 圖片推送的一些開發小Tips

iOS 圖片推送的一些開發小Tips

在這裡插入圖片描述 在這裡插入圖片描述

新版本產品大大要求增加圖文推送的功能。 由於專案中已經集成了推送功能,故本篇只講一下接入圖片推送的相關Tips。

新建NotificationService、NotificationContent的流程

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

NotificationService的作用

下載圖片/音訊/視訊檔案(注意:下載時間只有30秒、超時通過serviceExtensionTimeWillExpire方法進行迅速處理)。

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSString *subTitle = [[request.content.userInfo valueForKey:@"推送中自定義的字典欄位"] valueForKey:@"自定義副標題欄位"];
    NSString *attchUrl = [[request.content.userInfo valueForKey:@"推送中自定義的字典欄位"] valueForKey:@"自定義圖片欄位"];
    self.bestAttemptContent.subtitle = subTitle.length > 0 ? subTitle : @"";
    //1. 下載
    NSURL *url = [NSURL URLWithString:attchUrl];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            //2. 儲存資料, 不可以儲存到不存在的路徑
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"logo.png"];
            UIImage *image = [UIImage imageWithData:data];
            [UIImageJPEGRepresentation(image, 1) writeToFile:path options:NSAtomicWrite error:&err];
            //3. 新增附件
            UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"remote-atta1" URL:[NSURL fileURLWithPath:path] options:nil error:&err];
            if (attachment) {
                self.bestAttemptContent.attachments = @[attachment];
            }
        }
        self.contentHandler(self.bestAttemptContent);
    }];
    [task resume];
}

下載超時,則通過下面的方法,改成展示原有系統樣式。

- (void)serviceExtensionTimeWillExpire 
{
    self.contentHandler(self.bestAttemptContent);
}

如何使推送走NotificationService的處理?

服務端設定"mutable-content":1 ,這樣推送過來就會走NotificationService的處理邏輯。

{
  "aps":{
    "alert":{
      "title":"iOS 10 title",
      "subtitle":"iOS 10 subtitle",
      "body":"iOS 10 body"
    },
    "my-attachment":"http://img01.taopic.com/160317/240440-16031FU23937.jpg",
    "mutable-content":1,
    "category":"myNotificationCategory1",
    "sound":"default",
    "badge":3
  }
}

所有的操作都對了,為什麼還是不能走NotificationService的斷點?

  1. 首先說一下UNNotificationContentExtension的除錯:直接切換target,點選run即可。
  2. 而UNNotificationServiceExtension,因為該程式碼完全處於後臺執行(不像UNNotificationContentExtension可以由通知介面開啟觸發),像上面那種方式除錯還是不會走斷點,這就需要Debug -> Attach to process by pID or name: 在這裡插入圖片描述

在這裡插入圖片描述

NotificationContent作用

UNNotificationContentExtension用於自定義3D Touch後彈出的檢視,並且可以用UNNotificationAction自定義按鈕。

NotificationContent注意點

1、若想使用NotificationContent自定義3D Touch後彈出的檢視,必須讓服務端設定"category":"your categoryId"引數,這個引數在UNNotificationContentExtension的info.plist中設定,只有服務端配置的category和你本地設定的一樣,才會走你自定義的NotificationContent。並且category可以設定多個,即可以有多套自定義3D Touch UI。

在這裡插入圖片描述

2、如果普通推送,則"mutable-content","category"兩個引數都不要設定或為空。

3、NotificationService、NotificationContent都需要設定一下AllowArbitraryLoads為YES。

在這裡插入圖片描述