uniapp push 1.0版本
我用华为手机测试的离线消息,首先要保证在华为后台推送消息,uniapp离线打包的app能收到。
java后端推送
pom.xml依赖
<dependency>
<groupId>com.getui.push</groupId>
<artifactId>restful-sdk</artifactId>
<version>1.0.1.0</version>
</dependency>
java代码
package com.xx.xx.config.getui.service;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* <p> 个推 service </p>
*
* @author ocean
* @since 2024-05-06
*/
@Slf4j
@Service
public class GeTuiService {
@Resource
private GtApiConfiguration gtApiConfiguration;
public boolean pushSingle(String cid, String title, String body, String payload) {
PushApi pushApi = ApiHelper.build(gtApiConfiguration).creatApi(PushApi.class);
//根据cid进行单推
PushDTO<Audience> pushDTO = new PushDTO<>();
// 设置推送参数
pushDTO.setRequestId(System.currentTimeMillis() + "");
PushMessage pushMessage = new PushMessage();
pushDTO.setPushMessage(pushMessage);
GTNotification notification = new GTNotification();
pushMessage.setNotification(notification);
notification.setTitle(title);
notification.setBody(body);
notification.setClickType("payload");
notification.setPayload(payload);
notification.setChannelLevel("4");
notification.setCategory("CATEGORY_REMINDER");
//设置厂商推送消息参数
PushChannel pushChannel = new PushChannel();
pushDTO.setPushChannel(pushChannel);
AndroidDTO androidDTO = new AndroidDTO();
pushChannel.setAndroid(androidDTO);
Ups ups = new Ups();
androidDTO.setUps(ups);
ThirdNotification thirdNotification = new ThirdNotification();
ups.setNotification(thirdNotification);
thirdNotification.setTitle(title);
thirdNotification.setBody(body);
thirdNotification.setPayload(payload);
thirdNotification.setClickType("intent");
thirdNotification.setIntent("intent://io.dcloud.unipush/?#Intent;scheme=unipush;launchFlags=0x4000000;component=xx.xx.com/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=" + title + ";S.content=" + body + ";S.payload=" + payload + ";end");
// 设置接收人信息
Audience audience = new Audience();
pushDTO.setAudience(audience);
audience.addCid(cid);
// 进行cid单推
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
if (apiResult.isSuccess()) {
// success
log.info("发送信息成功" + apiResult);
return true;
} else {
// failed
log.info("发送信息失败" + apiResult);
return false;
}
}
}
注意事项:
component=xx.xx.com/io.dcloud.PandoraEntry;
中的xx.xx.com
替换为你自己的包名。
uniapp代码
App.vue
onLaunch(() => {
// 离线消息
plus.push.addEventListener("click", (msg) => {
const payload = msg.payload
// todo: payload业务逻辑
}, false)
// 在线消息
plus.push.addEventListener("receive", (msg) => {
const payload = msg.payload
// todo: payload业务逻辑
}, false)
})