uni-app webH5使用uinPush服务端推送消息无效

发布于 1周前 作者 ionicwang 来自 Uni-App

uni-app webH5使用uinPush服务端推送消息无效

示例代码:

'use strict';
const uniPush = uniCloud.getPushManager({
appId: "xxxx"
})
exports.main = async (event) => {
return  await uniPush.sendMessage({
"push_clientid":'xxxxx',
// "force_notification": true,
"title": 'GGGGG',
"content": '嗷嗷嗷啊',
"payload":{
"text":"哈哈哈哈哈"
}
})
};
return event;  

操作步骤:

  • 浏览器运行使用uinPush服务端云函数云函数推送消息无效使用透传消息,uni.onPushMessage能接收到但用不了plus.push.createMessage()方法创建本地通知。

预期结果:

  • uinPush服务端云函数能够推送消息

实际结果:

  • uinPush服务端云函数能无法推送消息使用透传消息,plus.push.createMessage()方法也无法创建本地通知

bug描述:

  • webH5浏览器运行使用uinPush服务端云函数推送消息无效只能推透传消息,uni.onPushMessage能接收到但用不了plus.push.createMessage()方法创建本地通知。

4 回复

plus. 开头的 api 只支持 APP 端


那就是不能搞了,h5在手机浏览器发推送消息?

那就是不能搞了,h5在手机浏览器发推送消息?

在使用 uni-app 开发 Web H5 应用时,如果你使用了 uinPush 服务端推送消息但发现无效,可能是由于以下几个原因导致的。以下是一些常见的排查步骤和解决方案:

1. 确认 uinPush 配置正确

确保你在 uni-app 项目中正确配置了 uinPush。通常需要在 manifest.json 文件中进行配置。例如:

{
  "app-plus": {
    "distribute": {
      "ios": {
        "uinPush": {
          "appKey": "your-app-key",
          "appSecret": "your-app-secret"
        }
      },
      "android": {
        "uinPush": {
          "appKey": "your-app-key",
          "appSecret": "your-app-secret"
        }
      }
    }
  }
}

2. 检查推送服务端代码

确保你的服务端代码正确调用了 uinPush 的 API 来发送消息。通常需要以下几个步骤:

  • 获取设备的 deviceTokenclientId
  • 调用 uinPush 的推送 API 发送消息。

示例代码(以 Node.js 为例):

const axios = require('axios');

const sendPushNotification = async (deviceToken, message) => {
  const url = 'https://api.uinpush.com/push';
  const data = {
    appKey: 'your-app-key',
    appSecret: 'your-app-secret',
    deviceToken: deviceToken,
    message: message
  };

  try {
    const response = await axios.post(url, data);
    console.log('Push notification sent:', response.data);
  } catch (error) {
    console.error('Error sending push notification:', error);
  }
};

// 调用函数发送推送
sendPushNotification('device-token', 'Hello, this is a test message!');

3. 检查设备是否注册成功

确保设备已经成功注册到 uinPush 服务。通常在客户端代码中需要调用 uinPush 的注册方法:

uni.getPushClientId({
  success: function(res) {
    console.log('Client ID:', res.cid);
  },
  fail: function(err) {
    console.error('Failed to get client ID:', err);
  }
});
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!