1

I have implemented NotificationService Extension in Objective-C as per Salesforce Swift sample code in documentation. The code works as I can intercept push messages, and can update title, body, but the code to download media doesn't seems to do anything. The log statements inserted in code, not executed. No exceptions were raised despite using @try blocks.

Any body, has successfully implemented rich notification in Objective-C.

Thanks

Here is the code, I wrote:

@implementation NotificationService
-(UNNotificationAttachment *) createMediaAttachment: (NSURL *) localMediaUrl
{
    CGRect clippingRect = CGRectZero;
    UNNotificationAttachment *mediaAttachment = nil;
    @try {
        NSError *error = nil;
        mediaAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachmentIdentifier" URL:localMediaUrl options:@{UNNotificationAttachmentOptionsThumbnailClippingRectKey: (NSDictionary *) CFBridgingRelease(CGRectCreateDictionaryRepresentation(clippingRect)), UNNotificationAttachmentOptionsThumbnailHiddenKey: @"false"} error:&error];
    } @catch (NSException *exception) {
    }
    return mediaAttachment;
}

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSString *mediaUrlString = request.content.userInfo[kMediaUrlKey] != nil ? request.content.userInfo[kMediaUrlKey] : nil;
    if (mediaUrlString != nil && [mediaUrlString length] > 0)
    {
        NSURL *mediaUrl = [NSURL URLWithString:mediaUrlString];
        if (![mediaUrl isFileURL] ) {
            NSURLSession *session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];
             __typeof__(self) __weak weakSelf = self;
            [[ session downloadTaskWithURL:mediaUrl completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error)  {
                BOOL useAlternateText = YES;
                if (error == nil) {
                    NSHTTPURLResponse *downloadResponse = (NSHTTPURLResponse *)response;
                    if (downloadResponse != nil && downloadResponse.statusCode >= 200 && downloadResponse.statusCode <= 299) {
                        NSString *fullPath = [NSString stringWithFormat:@"%@%@", location.path, mediaUrl.lastPathComponent];
                        NSURL *localMediaUrl = [[NSURL alloc] initFileURLWithPath: fullPath ];
                        @try {
                            // remove any existing file with the same name
                            NSError *error = nil;
                            BOOL removed = [[NSFileManager defaultManager] removeItemAtURL:localMediaUrl error:&error];
                            BOOL moved = [[NSFileManager defaultManager] moveItemAtURL:location toURL:localMediaUrl error:&error];
                            if (error == nil ) {
                                UNNotificationAttachment *mediaAttachment = [weakSelf createMediaAttachment:localMediaUrl];
                                if (mediaAttachment != nil ){
                                    weakSelf.bestAttemptContent.attachments = @[mediaAttachment];
                                    useAlternateText = NO;
                                }
                            }

                        } @catch (NSException *exception) {}
                    }
                }
                NSString *mediaAltText = request.content.userInfo[kMediaAltKey] != nil ? request.content.userInfo[kMediaAltKey] : nil;
                if (mediaAltText != nil) {
                    if ([mediaAltText length] > 0) {
                        weakSelf.bestAttemptContent.body = [NSString stringWithFormat:@"%@", mediaAltText]; //mediaAltText;
                    }
                }
                weakSelf.contentHandler(weakSelf.bestAttemptContent);
            }] resume];
        }
    }
    else {
        self.contentHandler(request.content);
        return;
    }
    self.contentHandler(self.bestAttemptContent);
}
3
  • Please share your service extension. – Bill Mote Aug 20 '20 at 21:37
  • Hi Bill, I have updated the post with service extension code. Thanks – Kais Ebraheem Aug 24 '20 at 21:10
  • I have passed your question along. – Bill Mote Aug 25 '20 at 12:31
0

When debugging this, are you able to get a value for mediaUrlString? I see that you are using a variable for accessing the userInfo dictionary with kMediaUrlKey. Is kMediaUrlKey == @"_mediaUrl"? Does your variable mediaUrl look as you would expect it to when you pass it to downloadTaskWithURL? Also try adding a NSLog in your catch statement and log the exception to see if that is giving you any issues. Also printing out your response status code.

1
  • Yes I can get the value of mediaUrlString - the key @"_mediaUrl" is specific to SF MobilePush rich notification media. Did add NSLog statements (not included with code) but they were not firing. The issue is only with the media attachment part. Thanks. – Kais Ebraheem Aug 25 '20 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.