欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS推送增加右側(cè)顯示圖Service Extension

 更新時(shí)間:2022年10月11日 16:48:46   作者:ZouNiMei  
這篇文章主要為大家介紹了iOS推送增加右側(cè)顯示圖Service Extension,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

本Demo推送使用的是極光推送(換成其他推送改動(dòng)也不大)極光文檔 極光Demo

先看下效果圖,在系統(tǒng)的推送彈窗右側(cè)增加了一個(gè)圖片

工程配置(一)

  • 首先需要一個(gè)已經(jīng)集成了極光推送,并且可以正常接收推送的工程(參考極光文檔again);
  • 配置主Target,如下截圖所示,勾選主Target的Background Modes;

  • 創(chuàng)建Service Extension,看下面的三圖;

  • 給創(chuàng)建好的PushExtension(子Target)配置Push Notifications,這一步操作就和主Target的配置推送一樣;

工程配置(二)集成JPushExtension

這一步是按照需求可選的,引入JPushExtension的目的是為了極光推送做統(tǒng)計(jì)

處理推送顯示的內(nèi)容

這是配置好的工程目錄,多了一個(gè)PushExtention文件夾

NotificationService.m文件的內(nèi)容改為如下

#import "NotificationService.h"
#import "JPushNotificationExtensionService.h"
@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    // 讀取圖片地址,并加載
    NSString *imgUrl = [NSString stringWithFormat:@"%@", self.bestAttemptContent.userInfo[@"imageUrl"]]; // ??圖片字段的key值需要跟后臺(tái)開(kāi)發(fā)統(tǒng)一
    if (imgUrl) {
        NSURL *fileURL = [NSURL URLWithString:imgUrl];
        [self downloadAndSave:fileURL handler:^(NSString *localPath) {
            if (localPath) {
                UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"myAttachment" URL:[NSURL fileURLWithPath:localPath] options:nil error:nil];
                self.bestAttemptContent.attachments = @[attachment];
            }
            [self apnsDeliverWith:request];
        }];
    } else {
        [self apnsDeliverWith:request];
    }
}
- (void)serviceExtensionTimeWillExpire {
    self.contentHandler(self.bestAttemptContent);
}
#pragma mark - 私有方法
- (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler {
    // 這里需要用系統(tǒng)網(wǎng)絡(luò)請(qǐng)求來(lái)下載圖片
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *localPath = nil;
        if (!error) {
            // 臨時(shí)文件夾路徑,APP沒(méi)有運(yùn)行時(shí)會(huì)自動(dòng)清除圖片,不會(huì)占用內(nèi)存
            NSString *localURL = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), fileURL.lastPathComponent];
            if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:localURL error:nil]) {
                localPath = localURL;
            }
        }
        handler(localPath);
    }];
    [task resume];
}
- (void)apnsDeliverWith:(UNNotificationRequest *)request {
    [JPushNotificationExtensionService jpushSetAppkey:@"本應(yīng)用在極光平臺(tái)的AppKey"];
    [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
        NSLog(@"apns upload success");
        self.contentHandler(self.bestAttemptContent);
    }];
}
@end

注意事項(xiàng)

如果傳了圖片地址卻還不顯示,不要驚慌,先請(qǐng)確保圖片別太大,而且可以使用NSURLSession下載,否則就會(huì)出現(xiàn)圖片不顯示的問(wèn)題。

以上就是iOS推送增加右側(cè)顯示圖Service Extension的詳細(xì)內(nèi)容,更多關(guān)于iOS 推送增加右側(cè)顯示圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論