[英]Rich push notification not working in iPhone 11 Pro Max in iOS
我正在嘗試在推送通知警報上顯示圖像。 這適用於 iPhone X、iPhone XR、iPhone 7 等其他設備。但不幸的是,這不適用於 iPhone 11 Pro Max。 我的客戶有 iPhone 11 Pro Max,因此我無法調試問題。
我從推送通知有效負載中獲取代碼並從該代碼下載圖像。
我創建了“通知服務擴展”的擴展
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler
{
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSArray *arr = [self.bestAttemptContent.body componentsSeparatedByString:@": "];
NSString *alertText = self.bestAttemptContent.body;
NSString *stickerMessage = nil;
if (arr.count > 1){
//Check if text is of sticker
if ([self isStickerMessage:arr[1]]) {
alertText = [NSString stringWithFormat:@"%@:",arr[0]];
stickerMessage = arr[1];
}
}
// check for media attachment, example here uses custom payload keys mediaUrl and mediaType
if (stickerMessage == nil)
{
[self contentComplete];
return;
}
NSCharacterSet* characterSet = [NSCharacterSet characterSetWithCharactersInString: @"[]"];
NSString* stickerName = [stickerMessage stringByTrimmingCharactersInSet: characterSet];
if (stickerName)
{
self.bestAttemptContent.body = alertText;
[self loadImagePath:stickerName completionHandler:^(NSString *picURL) {
if (picURL) {
[self loadAttachmentForUrlString:picURL
completionHandler: ^(UNNotificationAttachment *attachment) {
self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil];
[self contentComplete];
}];
}
else{
[self contentComplete];
}
}];
}
}
-(void)loadImagePath: (NSString *)stickerName
completionHandler:(void (^)(NSString *))completionHandler{
NSString *apiPath = [NSString stringWithFormat:@"%@%@",ApiPath,stickerName];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:apiPath]];
[self headerSetup:req];
//NSString *userID = [[NSUserDefaults standardUserDefaults] stringForKey: @"kUserKeyDefaultsKey"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
completionHandler(nil);
}else if (data){
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (dict) {
//NSURL* urlString = [NSURL URLWithString: dict[@"data"][@"image"][@"mdpi"]];
//NSLog(@"%@",urlString);
completionHandler(dict[@"data"][@"image"][[self scaleString]]);
}
}
else{
completionHandler(nil);
}
}];
[task resume];
}
- (void)loadAttachmentForUrlString:(NSString *)urlString
completionHandler:(void (^)(UNNotificationAttachment *))completionHandler
{
__block UNNotificationAttachment *attachment = nil;
__block NSURL *attachmentURL = [NSURL URLWithString:urlString];
NSString *fileExt = @".png";//[@"." stringByAppendingString:[urlString pathExtension]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:attachmentURL
completionHandler: ^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
if (error != nil)
{
NSLog(@"%@", error.localizedDescription);
}
else
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path
stringByAppendingString:fileExt]];
[fileManager moveItemAtURL:temporaryFileLocation
toURL:localURL
error:&error];
NSError *attachmentError = nil;
attachment = [UNNotificationAttachment attachmentWithIdentifier:[attachmentURL lastPathComponent]
URL:localURL
options:nil
error:&attachmentError];
if (attachmentError)
{
NSLog(@"%@", attachmentError.localizedDescription);
}
}
completionHandler(attachment);
}];
[task resume];
}
- (void)contentComplete
{
//[self.session invalidateAndCancel];
self.contentHandler(self.bestAttemptContent);
}
我不確定是什么問題,但我直接從 URL 下載了圖像,而不是使用 NSURLSessionDataTask & 這解決了問題。
以下是我寫的代碼
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler
{
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSArray *arr = [self.bestAttemptContent.body componentsSeparatedByString:@": "];
NSString *alertText = self.bestAttemptContent.body;
NSString *stickerMessage = nil;
if (arr.count > 1){
//Check if text is of sticker
if ([self isStickerMessage:arr[1]]) {
alertText = [NSString stringWithFormat:@"%@:",arr[0]];
stickerMessage = arr[1];
}
}
// check for media attachment, example here uses custom payload keys mediaUrl and mediaType
if (stickerMessage == nil)
{
[self contentComplete];
return;
}
NSCharacterSet* characterSet = [NSCharacterSet characterSetWithCharactersInString: @"[]"];
NSString* stickerName = [stickerMessage stringByTrimmingCharactersInSet: characterSet];
if (stickerName)
{
NSFileManager *fileManger = [NSFileManager defaultManager];
NSURL *documentsDirectory = [[fileManger URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];//FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
NSString *fileName = [NSString stringWithFormat:@"%@.png",stickerName];
NSURL *fileURL = [documentsDirectory URLByAppendingPathComponent:fileName];
self.bestAttemptContent.body = alertText;
NSString *picURL = [NSString stringWithFormat:@"https://stickerpipe.com/stk/emojiabc/%@_hdpi.png",stickerName];
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:picURL]];
if (data) {
if ([data writeToURL:fileURL atomically:true]) {
NSError *attachmentError = nil;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:fileName
URL:fileURL
options:nil
error:&attachmentError];
self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil];
}else{
NSLog(@"Data can't be written");
}
}
[self contentComplete];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.