uni-app下载文件创建推送,点击推送打开文件

  • 调用uni.downloadFile下载文件到本地,但是需要注意的是,downloadUrl只能请求资源地址,并且该方法只能下载文件到临时路径,如果重新安装,则文件会丢失。
  • 如果下载成功,调用uni.saveFile永久存储在本机中,如果存储成功,调用plus.push.createMessage在本地创建推送消息,并携带本机文件地址

此处需要注意点,调用openFile时,需要创建setTimeout包裹起来,时间最好写一秒,因为发现了,ios是可以正常打开,但是安卓点击推送后是没反应的无法打开。初步推测为安卓打卡openFile需要耗时。

uni.downloadFile({
        url: 'https://mobility-pwa-apac-qa.s3.ap-southeast-1.amazonaws.com/ordersearch/orderSummary_2731473.xls',
        success: (res) => {
            if (res.statusCode === 200) {
                uni.saveFile({
                    tempFilePath: res.tempFilePath,
                    success: function(res) {
                        plus.push.createMessage('download successfully', {
                            downloadFilePath: res.savedFilePath,
                        });
                    }
                });
            } else {
                plus.push.createMessage("Download Fail");
            }
        },
        fail: (err) => {
            console.log("download fail", err)
                plus.push.createMessage("Download Fail");
            }
});

onLoad: function() {
    plus.push.addEventListener('click', (result) => {
        const { payload } = result;
        if (payload?.downloadFilePath) {
            setTimeout(() => {
                plus.runtime.openFile(payload.downloadFilePath, {}, (err) => {
                    console.log("open error", err)
                })
            },1000)
        }
    });
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容