【问题标题】:Downloading an image for showing on a local notification not working下载用于在本地通知上显示的图像不起作用
【发布时间】:2020-02-22 05:52:28
【问题描述】:

我正在开发一个框架,该框架具有从 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);
}

【问题讨论】:

    标签: ios objective-c cocoa-touch unusernotificationcenter


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2016-11-08
      • 2020-09-22
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      相关资源
      最近更新 更多