【发布时间】:2018-08-20 19:52:45
【问题描述】:
我正在尝试更新 NativeScript 插件 nativescript-local-notifications 以在通知中显示图像。
这已经在 Android here 上运行,但我在尝试为 iOS 实现相同的东西时遇到了一些问题。
我对@987654323@中的方法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