uniapp push个推在线和离线消息监听,离线消息intent参数设置和获取,java后端推送

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)
	})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是华为离线推送参数配置示例: ```java String appId = "yourAppId"; // 应用ID String appSecret = "yourAppSecret"; // 应用秘钥 String tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token"; // 获取认证Token的URL String apiUrl = "https://api.push.hicloud.com/pushsend.do"; // Push消息下发API String accessToken = null; // 认证Token // 获取认证Token try { HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(tokenUrl); post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("grant_type", "client_credentials")); params.add(new BasicNameValuePair("client_id", appId)); params.add(new BasicNameValuePair("client_secret", appSecret)); post.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject jsonObject = new JSONObject(result); accessToken = jsonObject.getString("access_token"); } catch (Exception e) { e.printStackTrace(); } // 发送Push消息 try { HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(apiUrl); post.setHeader("Content-Type", "application/json;charset=utf-8"); post.setHeader("Authorization", "Bearer " + accessToken); JSONObject json = new JSONObject(); json.put("validate_only", false); // 是否只验证消息 json.put("message_type", "notify"); // 消息类型:notify-通知,message-透传消息 json.put("android", new JSONObject().put("hps", new JSONObject().put("msg", new JSONObject() .put("type", 3) // 消息类型:1-透传异步消息,3-通知栏消息 .put("body", new JSONObject() .put("title", "Push Title") // 通知栏消息标题 .put("content", "Push Content") // 通知栏消息正文 .put("badge", 1) // 角标 .put("sound", "default") // 声音 .put("click_action", new JSONObject().put("type", 1) // 点击通知后的行为:1-打开APP首页,2-打开自定义页面,3-打开URL .put("intent", "#Intent;compo=com.rvr/.Activity;S.W=U;end")) .put("extras", new JSONObject() .put("key1", "value1") // 自定义参数 .put("key2", "value2") .put("key3", "value3"))))))); json.put("token_list", new JSONArray().put("yourDeviceToken")); post.setEntity(new StringEntity(json.toString(), "utf-8")); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } ``` 其中,需要替换的参数有: - `yourAppId`:应用ID - `yourAppSecret`:应用秘钥 - `yourDeviceToken`:设备Token,可通过华为推送服务的SDK获取 另外,需要注意的是,华为推送服务的API与参数可能会有变更,以上代码仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值