diff --git a/lib/apis.js b/lib/apis.js index c8edfdc28f3dbce7dd9debae6c13a9beffa1f08f..18215f7b60146c9ece5106966233ddeea442b5f5 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -269,7 +269,8 @@ const plugin = [ 'invokePushCallback', 'getPushClientId', 'onPushMessage', - 'offPushMessage', + 'offPushMessage', + 'createPushMessage' ] const apis = [ diff --git a/packages/uni-cli-shared/lib/manifest.js b/packages/uni-cli-shared/lib/manifest.js index 323c69c0009cc3eb2df801807abc094c410e0553..7e0748a75c03e30020572bbc000d5ca50dbc6dac 100644 --- a/packages/uni-cli-shared/lib/manifest.js +++ b/packages/uni-cli-shared/lib/manifest.js @@ -1,4 +1,7 @@ const path = require('path') +const { + hasOwn +} = require('./util') const { getJson, @@ -98,6 +101,24 @@ function getH5Options (manifestJson) { return h5 } +function isEnableUniPushV1 (manifestJson, platform) { + if (!manifestJson) { + manifestJson = getManifestJson() + } + if (isEnableUniPushV2(manifestJson, platform)) { + return false + } + if (platform === 'app-plus') { + const platformOptions = manifestJson[platform] + const sdkConfigs = platformOptions && platformOptions.distribute && platformOptions.distribute.sdkConfigs + const push = sdkConfigs && sdkConfigs.push + if (push && hasOwn(push, 'unipush')) { + return true + } + } + return false +} + function isEnableUniPushV2 (manifestJson, platform) { if (!manifestJson) { manifestJson = getManifestJson() @@ -121,14 +142,15 @@ function isUniPushOffline (manifestJson) { const platformOptions = manifestJson['app-plus'] const sdkConfigs = platformOptions && platformOptions.distribute && platformOptions.distribute.sdkConfigs const unipush = sdkConfigs && sdkConfigs.push && sdkConfigs.push.unipush - return unipush && unipush.offline === true + return unipush && unipush.offline === true } module.exports = { getManifestJson, parseManifestJson, getNetworkTimeout, - getH5Options, + getH5Options, + isEnableUniPushV1, isEnableUniPushV2, isUniPushOffline } diff --git a/packages/uni-cli-shared/lib/platform.js b/packages/uni-cli-shared/lib/platform.js index 5ae62aab661b0392b4805d8a754fb238ec5532a3..fa2dcaf74610d2cc2b7cf9b48051b6a979820046 100644 --- a/packages/uni-cli-shared/lib/platform.js +++ b/packages/uni-cli-shared/lib/platform.js @@ -168,7 +168,7 @@ module.exports = { : 'import \'@dcloudio/uni-stat/dist/uni-stat.es.js\';' }, getPlatformPush () { - if (process.env.UNI_PUSH_V2_OFFLINE) { + if (process.env.UNI_PUSH_V2_OFFLINE || process.env.UNI_PUSH_V1) { return ';import \'@dcloudio/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.plus.es.js\';' } else if (process.env.UNI_PUSH_V2) { return ';import \'@dcloudio/vue-cli-plugin-uni/packages/uni-push/dist/uni-push.es.js\';' @@ -199,4 +199,4 @@ module.exports = { ] } } -} +} diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index e97e631e5b27cf4cc24af49f798f87dc7f421146..7b91755ed7978edc988f9b3bad31010c8f0effa0 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -21,7 +21,8 @@ if (process.env.UNI_INPUT_DIR && process.env.UNI_INPUT_DIR.indexOf('./') === 0) process.env.UNI_INPUT_DIR = process.env.UNI_INPUT_DIR || path.resolve(process.cwd(), defaultInputDir) const { - getManifestJson, + getManifestJson, + isEnableUniPushV1, isEnableUniPushV2, isUniPushOffline } = require('@dcloudio/uni-cli-shared/lib/manifest') @@ -47,6 +48,8 @@ if (isEnableUniPushV2(manifestJsonObj, process.env.UNI_PLATFORM)) { if (process.env.UNI_PLATFORM === 'app-plus' && isUniPushOffline(manifestJsonObj)) { process.env.UNI_PUSH_V2_OFFLINE = true } +} else if (isEnableUniPushV1(manifestJsonObj, process.env.UNI_PLATFORM)) { + process.env.UNI_PUSH_V1 = true } // 初始化全局插件对象 @@ -558,4 +561,4 @@ runByHBuilderX && console.log(uniI18n.__('compiling')) module.exports = { manifestPlatformOptions: platformOptions -} +} diff --git a/src/platforms/app-plus/service/api/device/get-app-authorize-setting.js b/src/platforms/app-plus/service/api/device/get-app-authorize-setting.js index 34493ceb9f53ef31b21e01aca9b058a93f2e7343..37f15d8d4b358107bab65241880843804f035560 100644 --- a/src/platforms/app-plus/service/api/device/get-app-authorize-setting.js +++ b/src/platforms/app-plus/service/api/device/get-app-authorize-setting.js @@ -1,10 +1,9 @@ -export function getAppAuthorizeSetting() { +export function getAppAuthorizeSetting () { const { getAppAuthorizeSetting } = weex.requireModule('plus') let appAuthorizeSetting = getAppAuthorizeSetting() try { - if (typeof appAuthorizeSetting === 'string') - appAuthorizeSetting = JSON.parse(appAuthorizeSetting) + if (typeof appAuthorizeSetting === 'string') { appAuthorizeSetting = JSON.parse(appAuthorizeSetting) } } catch (error) { } return appAuthorizeSetting -} \ No newline at end of file +} diff --git a/src/platforms/app-plus/service/api/device/get-system-setting.js b/src/platforms/app-plus/service/api/device/get-system-setting.js index 878e8d4f19aae00dfd01abe9f49edf8bb058e213..4a499b535a82688a2dd9bde8c31523253086d375 100644 --- a/src/platforms/app-plus/service/api/device/get-system-setting.js +++ b/src/platforms/app-plus/service/api/device/get-system-setting.js @@ -1,10 +1,9 @@ -export function getSystemSetting() { +export function getSystemSetting () { const { getSystemSetting } = weex.requireModule('plus') let systemSetting = getSystemSetting() try { - if (typeof systemSetting === 'string') - systemSetting = JSON.parse(systemSetting) + if (typeof systemSetting === 'string') { systemSetting = JSON.parse(systemSetting) } } catch (error) { } return systemSetting -} \ No newline at end of file +} diff --git a/src/platforms/app-plus/service/api/plugin/push.js b/src/platforms/app-plus/service/api/plugin/push.js index 637e46421ce0265c2b336b5f39e885991eba0c76..c866efd4646a3065c6793f892f1a06dbafec146c 100644 --- a/src/platforms/app-plus/service/api/plugin/push.js +++ b/src/platforms/app-plus/service/api/plugin/push.js @@ -1,7 +1,13 @@ import { + hasOwn +} from 'uni-shared' +import { + invoke, publish } from '../../bridge' - +import { + getAppAuthorizeSetting +} from '../device/get-app-authorize-setting' let onPushing let isListening = false @@ -63,3 +69,24 @@ export function offPush (params) { errMsg: 'offPush:ok' } } + +export function createPushMessage (params, callbackId) { + const setting = getAppAuthorizeSetting() + if (!hasOwn(setting, 'notificationAuthorized')) { + return invoke(callbackId, { + errMsg: 'createPushMessage:fail missing push module' + }) + } + if (setting.notificationAuthorized !== 'authorized') { + return invoke(callbackId, { + errMsg: 'createPushMessage:fail ' + setting.notificationAuthorized + }) + } + const options = Object.assign({}, params) + delete options.content + delete options.payload + plus.push.createMessage(params.content, params.payload, options) + invoke(callbackId, { + errMsg: 'createPushMessage:ok' + }) +}