富推送通知在iOS中的iPhone 11 Pro Max中不起作用
我正在尝试在推送通知警报上显示图像。这对于其他设备(如iPhoneX、iPhoneXR、iPhone7)来说效果很好。但不幸的是,这不适用于iPhone 11 Pro Max。我的客户端有iPhone 11 Pro Max,因此我无法调试该问题 我从推送通知负载中获取代码,并从该代码下载图像 我已创建“通知服务扩展”的扩展 } } }富推送通知在iOS中的iPhone 11 Pro Max中不起作用,ios,objective-c,apple-push-notifications,Ios,Objective C,Apple Push Notifications,我正在尝试在推送通知警报上显示图像。这对于其他设备(如iPhoneX、iPhoneXR、iPhone7)来说效果很好。但不幸的是,这不适用于iPhone 11 Pro Max。我的客户端有iPhone 11 Pro Max,因此我无法调试该问题 我从推送通知负载中获取代码,并从该代码下载图像 我已创建“通知服务扩展”的扩展 } } } 我不确定是什么问题,但我直接从URL下载了图像,而不是使用NSURLSessionDataTask&这解决了问题 以下是我编写的代码 - (void)didR
我不确定是什么问题,但我直接从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];
}
}
它不太可能是手机型号。很可能是网络问题或类似问题导致下载无法完成。为什么不创建一个TestFlight构建,将任何相关的错误消息放入通知中,以便您可以尝试查看发生了什么。
-(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);
}
- (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];
}
}