日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

通知实战 设置通知图片(iOS10以后的)

發布時間:2023/12/13 综合教程 44 生活家
生活随笔 收集整理的這篇文章主要介紹了 通知实战 设置通知图片(iOS10以后的) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

解釋兩個基本擴展(Notification ContentNotification Service

Notification Content其實是用來自定義長按通知顯示通知的自定義界面

Notification Service是用來處理遠程通知的,我們可以在遠程通知到來之際,我們在Notification Service

里面由30s的時間來處理這條通知的

首先使用Notification Service

1.創建擴展target

2. 這里會新建一個擴展的target,新的target需要新的ID,新的證書等(證書和id不會的請百度)

3.然后對主target進行設置

注意如果沒有用到后臺的播放什么的只選Remote notifications,免得被拒絕

4. 對擴展的target進行設置

5.這里用的是極光推送,需要極光的相關文件(需要文件的可以去極光開發中心獲取demo)

6.擴展target添加文件

7.當添加后為了可以訪問http,在對應的plist文件進行設置

8.設置完成之后就可以看代碼了

#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];
    //    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [NotificationService]", self.bestAttemptContent.title];
    NSString * attachmentPath = self.bestAttemptContent.userInfo[@"myAttachmentURL"];
    //if exist 
    if (attachmentPath) {
        //download
        NSURL *fileURL = [NSURL URLWithString:attachmentPath];
        [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)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler {
    
    NSURLSession * session = [NSURLSession sharedSession];
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *localPath = nil;
        if (!error) {
            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 {
    
    //please invoke this func on release version
    //[JPushNotificationExtensionService setLogOff];
    
    //service extension sdk
    //upload to calculate delivery rate
    [JPushNotificationExtensionService jpushSetAppkey:@"3a6190XXXXXXX28907"];
    [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
        NSLog(@"apns upload success");
        self.contentHandler(self.bestAttemptContent);
    }];
}

- (void)serviceExtensionTimeWillExpire {

    self.contentHandler(self.bestAttemptContent);
}
@end

其中的 "myAttachmentURL" 是自己定義的即可 (代碼請仔細閱讀,原理是獲取網絡圖片,然后再顯示)

9. 通過極光進行推送測試(目標平臺選擇開發環境即可,證書配置,請參考極光文檔)

10.設置參數, 測試開始完成推送."myAttachmentURL"是自己設置的哦!記得設置mutable-content

11.收到推送

12.Notification Service斷點調試說明

選擇target時并沒有我們的擴展target,需要點擊管理target進而點擊Autocreate Schemes Now

這個時候擴展的target出現了

好了可以運行了(會進行一次選擇,選擇我們的主app即可,運行后在NotificationService.m 文件中的斷點就可以捕捉到啦)

到這里Notification Service的使用說明就結束了,以后有補充的再寫啦!

總結

以上是生活随笔為你收集整理的通知实战 设置通知图片(iOS10以后的)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。