I'm trying to update the NativeScript plugin nativescript-local-notifications
to display images in the notifications.
That's already working on Android here, but I'm having some issues when trying to implement the same thing for iOS.
I have done some changes to the method schedulePendingNotificationsNew
in local-notifications.ios.ts
:
private static async schedulePendingNotificationsNew(pending: ScheduleOptions[]): Promise<void> {
...
content.userInfo = userInfoDict; // Not changed
if (options.image) {
const image: ImageSource = await imageSource.fromUrl(options.image);
const imageName: string = options.image.split('/').slice(-1)[0];
const imageExtension: "png" | "jpeg" | "jpg" = <"png" | "jpeg" | "jpg">imageName.split('.')[1]
const folderDest = fileSystemModule.knownFolders.temp();
const pathDest = fileSystemModule.path.join(folderDest.path, imageName);
console.log(`Image will be saved to = ${ pathDest }`);
const saved = image.saveToFile(pathDest, imageExtension);
console.log(`Image ${ saved ? '' : 'not' } saved. `);
console.log(`Image does ${ fileSystemModule.File.exists(pathDest) ? '' : 'not' } exist. `);
if (saved || fileSystemModule.File.exists(pathDest)) {
console.log('Attaching image...');
try {
const attachment = UNNotificationAttachment
.attachmentWithIdentifierURLOptionsError('attachment', NSURL.fileURLWithPath('file://' + pathDest), null);
// .attachmentWithIdentifierURLOptionsError('attachment', NSURL.fileURLWithPath(pathDest), null);
content.attachments = NSArray.arrayWithObject<UNNotificationAttachment>(attachement);
} catch(err) {
console.log('Attachment error ; ', err);
}
console.log('Image attached!');
}
}
const repeats = options.interval !== undefined; // Not changed
...
}
You can see I have created the attachment in two different ways:
const attachment = UNNotificationAttachment
.attachmentWithIdentifierURLOptionsError('attachment',
NSURL.fileURLWithPath('file://' + pathDest), null);
And:
const attachment = UNNotificationAttachment
.attachmentWithIdentifierURLOptionsError('attachment',
NSURL.fileURLWithPath(pathDest), null);
But none of them work, in both cases I get a text-only notification and these logs:
Image will be saved to = /var/mobile/Containers/Data/Application/.../Library/Caches/1974-lancia-stratos-hf-stradale-for-sale.jpg
Image not saved.
Image does not exist.
I'm testing it on an iPhone 7 and an iPhone 8 and the image I'm trying to save is this one: https://icdn-0.motor1.com/images/mgl/7WjgW/s3/1974-lancia-stratos-hf-stradale-for-sale.jpg