【问题标题】:Update NativeScript plugin nativescript-local-notifications to support images更新 NativeScript 插件 nativescript-local-notifications 以支持图像
【发布时间】:2018-08-20 19:52:45
【问题描述】:

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

这已经在 Android here 上运行,但我在尝试为 iOS 实现相同的东西时遇到了一些问题。

我对@9​​87654323@中的方法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

【问题讨论】:

  • @rmaddy 此代码已编译为本机代码,因此知道如何在 Swift 或 Objective-C 中正确实现此代码的人也可以提供帮助,因为解决方案可能只是搜索NS 中的等价类和方法。

标签: ios nativescript angular2-nativescript


【解决方案1】:

我通过强制将图像保存为png 来修复它:

export class LocalNotificationsImpl extends LocalNotificationsCommon implements LocalNotificationsApi {

    ...

    private static guid() {
        // Not the best, but will it will do. See https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript

        const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);

        return `${ s4() }${ s4() }-${ s4() }-${ s4() }-${ s4() }-${ s4() }${ s4() }${ s4() }`;
    }

    private static getImageName(imageURL: string = "", extension: "png" | "jpeg" | "jpg" = "png"): [string, string] {
        const name: string = imageURL.split(/[\/\.]/).slice(-2, -1)[0] || LocalNotificationsImpl.guid();

        return [name, `${ name }.${ extension }`];
    }

    ...

    private static async schedulePendingNotificationsNew(pending: ScheduleOptions[]): Promise<void> {

        ...

        const imageURL: string = options.image;

        if (imageURL) {
            const image: ImageSource = await imageSource.fromUrl(imageURL);
            const [imageName, imageNameWithExtension] = LocalNotificationsImpl.getImageName(imageURL, "png");
            const path: string = fileSystemModule.path.join(
                fileSystemModule.knownFolders.temp().path,
                LocalNotificationsImpl.getImageName(imageURL, "png"),
            );

            const saved = image.saveToFile(path, "png");

            if (saved || fileSystemModule.File.exists(path)) {                
                try {
                    const attachment = UNNotificationAttachment
                        .attachmentWithIdentifierURLOptionsError('attachment', NSURL.fileURLWithPath(path), null);

                    content.attachments = NSArray.arrayWithObject<UNNotificationAttachment>(attachment);
                } catch(err) {}
            }
        }

        ...

    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多