uniappx服务端推送消息配置
一.前置条件
1.申请Dcloud 开发者账号(https://www.dcloud.io/ )
2.HBuilder安装
3.安装模拟器
4.uni-push 2.0 文档(https://uniapp.dcloud.net.cn/unipush-v2.html )
5.创建一个uniappx项目

二.配置步骤
1.确认AppID
打开项目中 manifest.json 文件,确认AppId是否存在,若不存在则点击右侧 重新获取 按钮(此处可能需要登录Dcloud账号),会生成AppID

2.构建项目生成证书
点击 Hbuilder 菜单 运行 》 运行到手机或模拟器 》 制作自定义调试基座

点击打包,会出现打包校验提示,继续打包即可,随即会在控制台打印相关信息,此处等待时间可能较长,我们继续推进下一步,打包后台运行即可。

3.新建uniCloud
登录Dcloud 开发者中心,点击左侧uniCloud...

在新标签页中点击右上角新建服务空间,按提示完成即可,例子中建立一个叫uniapp-hello 的服务空间(取名仅作区分,无其他含义,视自己习惯命名即可);

4.创建应用信息推送
回到Dcloud开发者管理页面,点击左侧uni-push > 2.0(支持全段推送) > 应用信息

点击当前应用下拉框,选择我们需要推送的应用

选择平台视业务而定,此处示例仅勾选Android

点击选择Android包名,若包名不存在,则需等待上一步打包结束后刷新当前页面重新选择,
再添加云服务空间,选中上一步创建的空间即可,最后点击开通应用

5.创建云函数
在项目目录下的uniCloud 》coudfunctions 目录右键,选择新建云函数/云对象(若没有uniCloud目录可在项目根目录上右键,选择 创建uniCloud云开发环境 )

填写函数名点击创建即可

随后替换新建函数下的index.js 和 package.json 内容

index.js新内容如下(需替换第二行中自己的appId):
'use strict';
const uniPush = uniCloud.getPushManager({appId:"__UNI__XXXXX"})
exports.main = async (event, context) => {
const body = JSON.parse(event.body);
return await uniPush.sendMessage({
"push_clientid": body.cid,
"title": body.title,
"content": body.content,
"payload": body.data
})
};
package.json新内容如下:
{
"name": "photo_push",
"version": "1.0.0",
"description": "",
"main": "index.js",
"extensions": {
"uni-cloud-push": {}
},
"author": ""
}
6.增加uni-push 能力
在Hbuilder中打开manifest.json,勾选uni-push(消息推送)

在新建立的photo-push 目录上右键,选择 管理公共模块或扩展库依赖

选中统一推送服务,点击确定

7.增加扩展库依赖表
在database目录下增加以下依赖表文件(文件内容见结尾附件)

在 opendb-device.index.json 右键 》 初始化云数据库索引
剩余其他三个文件 右键 》 上传DB schema
8.上传部署云函数
随后在photo-push 目录点击 右键 》 上传部署,等待上传完成即可

9.云函数URL化
回到第三步创建发unicloud服务空间,点击 服务空间 名称进入详情页
在左侧 云函数/对象列表找到创建的云函数

点击函数名进入详情页,页面底部 云函数URL化编辑配置函数名 /sendMessage
注:每个服务空间内函数名不可重复,且URL路径不可出现重复部分

10.项目增加消息监听
在项目App.uvue中 onLaunch()生命函数中增加监听代码

uni.onPushMessage(res => {
console.log("监听消息:", res)
if (res.type == "click") {
console.log("点击消息:" + res)
}
if (res.type == "receive") {
console.log("收到APP消息" + res.data);
// 创建本地通知栏消息
uni.createPushMessage({
title: res.data.title as string,
content: res.data.content as string,
payload: res.data.payload
})
}
})
11.启动项目
启动模拟器,此处以 网易Mumu模拟器 演示
Hbuilder 菜单点击 运行 》 运行到手机或模拟器 》 运行到Android App 基座
选择设备后运行,等待控制台编译完成

控制台选择 连接云端云函数

12.发起调用
使用云函数URL发起调用

效果如图

请求参数说明:
{
"cid": ["02e3c939927d45df1028b274e493488c"], // 设备ID,长度不超过500
"title": "绿12",
"content": "收到消息12",
"data": { // data 为自定义业务参数,该字段可不传
"type": "messageList"
}
}
附件
●opendb-device.index.json
[
{
"IndexName": "index_device_id",
"MgoKeySchema": {
"MgoIndexKeys": [
{
"Name": "device_id",
"Direction": "1"
}
],
"MgoIsUnique": true
}
}
]
●opendb-device.schema.json
{
"bsonType": "object",
"required": [],
"permission": {
"read": false,
"create": true,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"appid": {
"bsonType": "string",
"description": "DCloud appid"
},
"device_id": {
"bsonType": "string",
"description": "设备唯一标识"
},
"vendor": {
"bsonType": "string",
"description": "设备厂商"
},
"push_clientid": {
"bsonType": "string",
"description": "推送设备客户端标识"
},
"imei": {
"bsonType": "string",
"description": "国际移动设备识别码IMEI(International Mobile Equipment Identity)"
},
"oaid": {
"bsonType": "string",
"description": "移动智能设备标识公共服务平台提供的匿名设备标识符(OAID)"
},
"idfa": {
"bsonType": "string",
"description": "iOS平台配置应用使用广告标识(IDFA)"
},
"imsi": {
"bsonType": "string",
"description": "国际移动用户识别码(International Mobile Subscriber Identification Number)"
},
"model": {
"bsonType": "string",
"description": "设备型号"
},
"platform": {
"bsonType": "string",
"description": "平台类型"
},
"uni_platform": {
"bsonType": "string",
"description": "uni-app 运行平台,与条件编译平台相同。"
},
"os_name": {
"bsonType": "string",
"description": "ios|android|windows|mac|linux "
},
"os_version": {
"bsonType": "string",
"description": "操作系统版本号 "
},
"os_language": {
"bsonType": "string",
"description": "操作系统语言 "
},
"os_theme": {
"bsonType": "string",
"description": "操作系统主题 light|dark"
},
"pixel_ratio": {
"bsonType": "string",
"description": "设备像素比 "
},
"network_model": {
"bsonType": "string",
"description": "设备网络型号wifi\/3G\/4G\/"
},
"window_width": {
"bsonType": "string",
"description": "设备窗口宽度 "
},
"window_height": {
"bsonType": "string",
"description": "设备窗口高度"
},
"screen_width": {
"bsonType": "string",
"description": "设备屏幕宽度"
},
"screen_height": {
"bsonType": "string",
"description": "设备屏幕高度"
},
"rom_name": {
"bsonType": "string",
"description": "rom 名称"
},
"rom_version": {
"bsonType": "string",
"description": "rom 版本"
},
"location_latitude": {
"bsonType": "double",
"description": "纬度"
},
"location_longitude": {
"bsonType": "double",
"description": "经度"
},
"location_country": {
"bsonType": "string",
"description": "国家"
},
"location_province": {
"bsonType": "string",
"description": "省份"
},
"location_city": {
"bsonType": "string",
"description": "城市"
},
"create_date": {
"bsonType": "timestamp",
"description": "创建时间",
"forceDefaultValue": {
"$env": "now"
}
},
"last_update_date": {
"bsonType": "timestamp",
"description": "最后一次修改时间",
"forceDefaultValue": {
"$env": "now"
}
}
},
"version": "0.0.1"
}
●opendb-tempdata.schema.json
{
"bsonType": "object",
"required": ["value", "expired"],
"permission": {
"read": false,
"create": false,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"value": {
"description": "值"
},
"expired": {
"description": "过期时间",
"bsonType": "timestamp"
}
}
}
●uni-id-device.schema.json
{
"bsonType": "object",
"required": [
"user_id"
],
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"user_id": {
"bsonType": "string",
"description": "用户id,参考uni-id-users表"
},
"ua": {
"bsonType": "string",
"description": "userAgent"
},
"uuid": {
"bsonType": "string",
"description": "设备唯一标识(需要加密存储)"
},
"os_name": {
"bsonType": "string",
"description": "ios|android|windows|mac|linux "
},
"os_version": {
"bsonType": "string",
"description": "操作系统版本号 "
},
"os_language": {
"bsonType": "string",
"description": "操作系统语言 "
},
"os_theme": {
"bsonType": "string",
"description": "操作系统主题 light|dark"
},
"vendor": {
"bsonType": "string",
"description": "设备厂商"
},
"push_clientid": {
"bsonType": "string",
"description": "推送设备客户端标识"
},
"imei": {
"bsonType": "string",
"description": "国际移动设备识别码IMEI(International Mobile Equipment Identity)"
},
"oaid": {
"bsonType": "string",
"description": "移动智能设备标识公共服务平台提供的匿名设备标识符(OAID)"
},
"idfa": {
"bsonType": "string",
"description": "iOS平台配置应用使用广告标识(IDFA)"
},
"model": {
"bsonType": "string",
"description": "设备型号"
},
"platform": {
"bsonType": "string",
"description": "平台类型"
},
"create_date": {
"bsonType": "timestamp",
"description": "创建时间",
"forceDefaultValue": {
"$env": "now"
}
},
"last_active_date": {
"bsonType": "timestamp",
"description": "最后登录时间"
},
"last_active_ip": {
"bsonType": "string",
"description": "最后登录IP"
}
},
"version": "0.0.1"
}