写的一个小测试,直接调用云函数就行
云函数:
'use strict';
const uniPush = uniCloud.getPushManager({
appId: "UNI56C8C3E"
})
exports.main = async (event) => {
let obj = JSON.parse(event.body)
console.log(obj.cids);
const res = await uniPush.sendMessage({
"push_clientid": obj.cids, // 设备id,支持多个以数组的形式指定多个设备,如["cid-1","cid-2"],数组长度不大于1000。服务器没有接收到cid,会被认为是群发,而群发是每分钟5次的限制。
"title": obj.title, // 标题
"content": obj.content, // 内容
"payload": obj.data, // 数据
"force_notification": true, // 服务端推送 需要加这一句 。true表示客户端会对在线消息自动创建“通知栏消息”
"request_id": obj.request_id, //请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
"options": obj.options
})
return res //一定要return回去
};
java:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class UniPushTest {
public static void main(String[] args) {
// 目标URL
String url = "云函数url";
// 创建JSON数据
ObjectMapper objectMapper = new ObjectMapper();
try {
String jsonData = objectMapper.writeValueAsString(new DataEntity(
"bedc8d5c7ac62044c00b3a378804c050",
"重庆天气",
"沙坪坝四十摄氏度"
));
// 创建HttpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpPost实例
HttpPost httpPost = new HttpPost(url);
// 设置请求头,指定发送的数据类型为JSON
httpPost.setHeader("Content-type", "application/json");
// 设置请求体
StringEntity entity = new StringEntity(jsonData, ContentType.APPLICATION_JSON.withCharset("UTF-8"));
httpPost.setEntity(entity);
// 发送请求并获取响应
CloseableHttpResponse response = httpClient.execute(httpPost);
// 读取响应内容
try {
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
String result = EntityUtils.toString(responseEntity);
System.out.println(result);
}
} finally {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
实体类:
import lombok.Data;
@Data
public class DataEntity {
private String cids;
private String title;
private String content;
public DataEntity(String cids, String title, String content) {
this.cids = cids;
this.title = title;
this.content = content;
}
}
4 个回复
DCloud_UNI_Anne
参考云函数/云对象URL化
阿凡提巴士
解决了没,我的业务服务器也需要同样的操作,不能通过客户端调用推送,我的业务需要后端业务服务器上调用推送(java的后台) 求指导 研究好几天了研究不明白
~~拜谢!!!!!一个网名
我靠,那就是如果不懂unicloud就不能使用了?
1***@163.com
写的一个小测试,直接调用云函数就行
云函数:
'use strict';
const uniPush = uniCloud.getPushManager({
appId: "UNI56C8C3E"
})
exports.main = async (event) => {
let obj = JSON.parse(event.body)
console.log(obj.cids);
const res = await uniPush.sendMessage({
"push_clientid": obj.cids, // 设备id,支持多个以数组的形式指定多个设备,如["cid-1","cid-2"],数组长度不大于1000。服务器没有接收到cid,会被认为是群发,而群发是每分钟5次的限制。
"title": obj.title, // 标题
"content": obj.content, // 内容
"payload": obj.data, // 数据
"force_notification": true, // 服务端推送 需要加这一句 。true表示客户端会对在线消息自动创建“通知栏消息”
"request_id": obj.request_id, //请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失
"options": obj.options
})
return res //一定要return回去
};
java:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class UniPushTest {
}
实体类:
import lombok.Data;
@Data
public class DataEntity {
}