代码之家  ›  专栏  ›  技术社区  ›  Danziger

更新NativeScript插件NativeScript本地通知以支持图像

  •  1
  • Danziger  · 技术社区  · 6 年前

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

    here ,但我在尝试为iOS实现相同的功能时遇到了一些问题。

    schedulePendingNotificationsNew 在里面 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
    
        ...
    }
    

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

    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 回复  |  直到 6 年前
        1
  •  0
  •   Danziger    6 年前

    我通过强制将图像保存为 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 .