下载图像以显示在本地通知中不起作用

问题描述 投票:0回答:1

我正在开发一个框架,该框架具有从FCM接收消息并将其转换为本地通知的功能。在我收到的键之一中,有一个指向图像的URL。因此,在Apple文档中进行了一些研究,发现该图像应该存储在设备中(我正在使用模拟器),然后可以使用它,因此,我实现了一种下载随机图像(.png)的方法。此方法工作正常,我对其进行了测试,并且图像位于指定的位置,问题是,当我在调用attachmentWithIdentifier:identifier URL: options: error:方法时从NSError *指针打印userInfo字典时,得到此信息>

{NSLocalizedDescription = "Invalid attachment file URL";}

我附上我的代码:

UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
if (settings.alertSetting == UNNotificationSettingEnabled)
{

    NSDictionary * remoteMessageData = [remoteMessage appData];

    NSString * imageHttpUrl = [remoteMessageData objectForKey:@"im"];

    NSURL * imageURL = [self getStorageFilePath:imageHttpUrl];

    [self downloadImageFromURL:imageHttpUrl withFullPath:imageURL.absoluteString withCompletitionHandler:^(NSHTTPURLResponse *httpResponse) {

        UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];

            content.title = [remoteMessageData objectForKey:@"ti"];
            content.body = [remoteMessageData objectForKey:@"bd"];

        if(httpResponse != nil)
        {
            if (httpResponse.statusCode == 200)
            {
                NSString * identifier = @"ImageIdentifier";
                NSArray<UNNotificationAttachment*> * attachments = [NSArray arrayWithObjects:[self getAttachments:imageURL withIdentifier:identifier], nil] ;
                content.attachments = attachments;
            }

        }

        content.userInfo = remoteMessageData;

        UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];

        //Create and register a request notification
        NSString * uuidString = [[NSUUID UUID] UUIDString];

        UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:uuidString content:content trigger:trigger];


        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            //Handle error
            if(error != nil)
            {
                NSLog(@"Dictionary: %@",[error userInfo]);
            }
        }];



- (NSURL *) getStorageFilePath : (NSString *)imageStringURL{

if(imageStringURL == nil)
{
    return nil;
}

NSURL * imageURL = [NSURL URLWithString:imageStringURL];

NSString * fileName = [imageURL lastPathComponent];

NSLog(@"fileName %@",fileName);

NSArray * systemPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"systemPaths %@",systemPaths);

NSString * tempDirectoryStringPath = [[[NSURL URLWithString:[systemPaths objectAtIndex:0]] absoluteString] stringByAppendingString:@"/"];

NSString * fullPath = [tempDirectoryStringPath stringByAppendingString:fileName];

NSLog(@"Full PATH: %@",fullPath);

return [NSURL URLWithString:fullPath];}

-(void) downloadImageFromURL : (NSString *)httpURL withFullPath:(NSString * )fullPath withCompletitionHandler:(void (^) (NSHTTPURLResponse * httpResponse) )taskResult{

if(httpURL == nil || fullPath == nil )
{
    taskResult(nil);
    return;
}

NSString *strImgURLAsString = httpURL;

strImgURLAsString = [strImgURLAsString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSURL * imgURL = [NSURL URLWithString:strImgURLAsString];

NSLog(@"Full PATH inside downloading: %@",fullPath);

NSURLSessionDataTask * downloadPhotoTask = [[NSURLSession sharedSession] dataTaskWithURL:imgURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;

    if (httpResponse.statusCode == 200)
    {
        [data writeToFile:fullPath atomically:YES];



        NSLog(@"Download run successfully");


    }else{
        NSLog(@"Download could not be completed");


    }

    taskResult(httpResponse);

} ];

[downloadPhotoTask resume];


  }

- (UNNotificationAttachment *) getAttachments: (NSURL *)attachmentURL withIdentifier : (NSString*)identifier
{

    NSError * error;

    UNNotificationAttachment * icon =  [UNNotificationAttachment attachmentWithIdentifier:identifier URL: attachmentURL options:nil error:&error];

    NSLog(@"Icon is : %@",[error userInfo]);

    UNNotificationAttachment* attachments = icon;

    return (attachments);
}

我正在开发一个框架,该框架具有从FCM接收消息并将其转换为本地通知的功能。在我收到的键之一中,有一个指向图像的URL。所以做一些...

ios objective-c cocoa-touch unusernotificationcenter
1个回答
0
投票

我可以使它工作。我使用的是[NSURL URLWithString:fullPath] NSURL,但我应该使用[NSURL fileURLWithPath:fullPath],但在方法downloadImageFromURL中,请继续使用不带fileURLWithPath:方法格式的字符串URL(这将导致图像无法下载)。

© www.soinside.com 2019 - 2024. All rights reserved.