[英]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.