有没有大哥接过的,求求了云函数是这样,ios离线死活不行 到底哪里写错了,已经云打包基座了也就是不行,插进也进去了,现在只有推送声音没有铃声 很急!!!
use strict';
// const uniPush = uniCloud.getPushManager({appId:"xxxxx"}) //注意这里需要传入你的应用appId
const CLUE_NOTICE_TYPES = [xx, x];
const IOS_PUSH_SOUND = 'xxx.caf';
const DEFAULT_XM_CHANNEL_ID = 'xxx';
function getPushMeta(payload) {
if (!payload) {
return null;
}
if (Number(payload.moduleType) === 2000 && CLUE_NOTICE_TYPES.indexOf(Number(payload.noticeType)) !== -1) {
return {
ringCode: 'clue_audio1',
androidChannelId: 'xxxx',
iosSound: IOS_PUSH_SOUND
};
}
return null;
}
function normalizePushOptions(options, pushMeta) {
if (!options) {
return options;
}
const nextOptions = Object.assign({}, options);
if (nextOptions.HW) {
const nextHWOptions = Object.assign({}, nextOptions.HW);
delete nextHWOptions['/message/android/notification/default_sound'];
delete nextHWOptions['/message/android/notification/channel_id'];
delete nextHWOptions['/message/android/notification/sound'];
delete nextHWOptions['/message/android/notification/importance'];
nextOptions.HW = nextHWOptions;
}
// 非特殊铃声类型时,移除小米自定义声音配置,只保留默认通知通道。
if (!pushMeta && nextOptions.XM) {
nextOptions.XM = {
'/extra.channel_id': DEFAULT_XM_CHANNEL_ID
};
}
return nextOptions;
}
exports.main = async (event, context) => {
//event为客户端上传的参数
console.log('event : ', event)
let body = event.body;
if (event.isBase64Encoded) {
body = Buffer.from(body);
}
if(!body) {
return { message: 'no body'};
}
const param = JSON.parse(body);
const pushMeta = getPushMeta(param.payload);
const payload = Object.assign({}, param.payload || {}, pushMeta || {});
const options = normalizePushOptions(param.options, pushMeta);
const sound = pushMeta && pushMeta.iosSound;
// 云函数是通用的,和项目没有直接的关系。通过传递 appId,可以为多个项目进行推送
const uniPush = uniCloud.getPushManager({
appId: param.appId
});
const message = {
"push_clientid": param.push_clientid,
"title": param.title,
"content": param.content,
"payload": payload,
"force_notification": false,
"request_id": param.request_id,
"badge": param.badge,
"options": options,
};
if (sound) {
message.sound = sound;
}
return await uniPush.sendMessage(message);
//返回数据给客户端
return event
};
2 个回复
1***@163.com (作者)
在线可以离线就是不行
Ask小助手
看到你的代码和问题,iOS 离线推送自定义铃声不生效,这是个常见问题。我们来逐步排查。
核心问题定位
你的云函数代码逻辑看起来是正确的,问题很可能出在 iOS 端的配置 上。
iOS 离线推送自定义铃声的必要条件
1. 插件配置检查
你使用的是官方的 自定义推送铃声和渠道 插件,iOS 端需要特别注意:
nativeplugins/DCloud-PushSound/ios/目录下,放入铃声文件.caf格式,且时长不超过 30 秒pushsound.caf2. 音频文件转换(仅 Mac 系统)
如果你在 Windows 上开发,需要找工具将音频转为 caf 格式。Mac 系统终端命令:
3. 多个音频文件配置
如果你需要使用多个不同的铃声文件(如你的代码中有
ringCode逻辑),需要:nativeplugins/DCloud-PushSound/ios/目录package.json,在resource节点中填写音频文件名称4. 云函数中 sound 字段
你的云函数中
iosSound的值是xxx.caf,确保:.caf)"sound": "xxx.caf"你的代码检查
看你的云函数逻辑:
这部分逻辑没问题,会正确设置 sound 字段。但请确认:
IOS_PUSH_SOUND = 'xxx.caf'这个文件名与实际放入插件的文件名完全一致CLUE_NOTICE_TYPES和moduleType的判断逻辑是否正确触发排查步骤
nativeplugins/DCloud-PushSound/ios/目录下有正确的.caf文件已知限制
根据社区反馈,iOS 离线推送的自定义铃声必须通过 APNs 的 sound 字段传递,且铃声文件必须预置在 App 包中。在线推送可以通过前端
uni.createPushMessage配合uni.createInnerAudioContext播放自定义音频,但离线推送只能依赖 APNs 的 sound 配置。如果以上步骤都确认无误仍不生效,建议检查:
需要我进一步帮你检查哪个环节吗?
要回复问题请先登录或注册
公告
更多>相关问题