更新NativeScript插件nativescript-local-notifications以支持图像

时间:2018-08-20 19:52:46

标签: ios nativescript angular2-nativescript

我正在尝试更新NativeScript插件nativescript-local-notifications,以在通知中显示图像。

该功能已在Android here上运行,但是在尝试为iOS实现相同功能时遇到了一些问题。

我对local-notifications.ios.ts中的方法schedulePendingNotificationsNew做了一些更改:

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

    ...
}

您可以看到我以两种不同的方式创建了附件:

const attachment = UNNotificationAttachment
    .attachmentWithIdentifierURLOptionsError('attachment', 
        NSURL.fileURLWithPath('file://' + pathDest), null);

并且:

const attachment = UNNotificationAttachment
    .attachmentWithIdentifierURLOptionsError('attachment',
        NSURL.fileURLWithPath(pathDest), null);

但是它们都不起作用,在两种情况下,我都会收到纯文本通知和以下日志:

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.

我正在iPhone 7和iPhone 8上对其进行测试,而我要保存的图像就是这个图像:https://icdn-0.motor1.com/images/mgl/7WjgW/s3/1974-lancia-stratos-hf-stradale-for-sale.jpg

1 个答案:

答案 0 :(得分:0)

我通过强制将图像另存为Home来修复它:

png

如果您有兴趣,这些更改已合并到my fork of the plugin中,并且有一个PR open in the official one