Uni-app implements push Uni-push (Android)

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

        Domestic Android Push is a chaotic world, because Google’s Push service FCM is blocked, so some domestic Android mobile phone manufacturers have made their own push, such as Huawei, Xiaomi, OPPO, VIVO, Meizu, etc., but there are still many domestic Mobile phone manufacturers have not provided an official push plan. A three-party independent company, such as a push, provides an independent push solution.
Before uniPush, if only three-party push was used, the push process could not be kept alive on many domestic mobile phones due to power-saving settings, resulting in failure to push.
However, if the official Push SDK of each Android phone is integrated and developed, the workload will be huge with so many platforms, and the management and maintenance will be very troublesome.

uniPush solves this problem, developers only need to develop once. The system will automatically select the most reliable push channel on different mobile phones to send push messages to ensure the delivery rate.

UniPush not only reduces the development cost, but also improves the push delivery rate, and it is free, which is the best solution for current push.


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is UniPush?

        UniPush is an integrated unified push service launched by DCloud and Getui. It has built-in system-level push and third-party push such as Apple, Huawei, Xiaomi, OPPO, VIVO, Meizu, Google FCM and other mobile phone manufacturers.

2. Use steps

1. Open the Unipush push service

        In this HBuilderX project, open manifest.json, select "App Module Configuration", find the Push push function configuration, and check "UniPush (integrate system push and individual push from various mobile phone manufacturers)".

 Click the checked uniPush configuration to enter the DCloud Developer Center

**Note:** When applying for activation, you need to ensure that the input must be consistent with the configuration when packaging, otherwise the push message may not be received. Android包名  Android应用签名 (签名信息可点击更多查看具体操作)

If you have opened Uni Push, you will see the following page:

 

Open the link Developer Center https://dev.dcloud.net.cn/#/pages/app/push/thirdparty

Uni Push integrates and unifies the system-level push of various mobile phone manufacturers, and currently supports Meizu, OPPO, Huawei, Xiaomi, and VIVO. If you need to use vendor push, you need to apply on the developer platform of each vendor first.
After the application is approved, the manufacturer will provide relevant push parameters, which need to be configured in the backend of the DCloud Developer Center.
Click the vendor push settings to select the current application project page as shown below:

Since the company's APP needs to be launched on Huawei, open the AppGallery Connect website and select 我的项目. Open the project just created, and check the corresponding Huawei AppID and Huawei SecretKey in the application information. This information will be used in subsequent steps, as shown in the following figure:

For the specific process of other manufacturers, click  the manufacturer's application activation guide - Getui Document Center to view details.

2. The java server sends the message content

        First of all, you need to obtain the AppId, AppKey, and MasterSecret parameters, log in to the DCloud Developer Center , obtain them on the "Application Configuration" page under "Uni Push", import the maven library file, push it online, push it offline, and package it in Java-sdk Rest-V2 vendor push example:

Complete server vendor push tutorial reference: https://docs.getui.com/getui/server/rest_v2/common_args/

        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-http</artifactId>
            <version>4.1.2.1</version>
        </dependency>

        <repository>
            <id>getui-nexus</id>
            <url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
        </repository>
public AjaxResult info( String commonApiModel ) {
        CommonInterfaceModel model = jsonToObject(commonApiModel,key,secret);
        if(model.isCheck()) {
            Order appPush = JSONObject.parseObject(model.getParameter(),Order.class);
            IGtPush push = new IGtPush(url, appKey, masterSecret);

            Style0 style = new Style0();
            // STEP2:设置推送标题、推送内容
            style.setTitle(appPush.getOdTitle());
            style.setText(appPush.getOdContent());
            // 设置推送图标
//            style.setLogo("./src/main/resources/static/push.png");
            // 配置通知栏网络图标
//            style.setLogoUrl("");
            // STEP3:设置响铃、震动等推送效果
            // 设置响铃
            style.setRing(true);
            // 设置震动
            style.setVibrate(true);

            // STEP4:选择通知模板
            NotificationTemplate template = new NotificationTemplate();
            template.setTransmissionType(1);
            template.setAppId(appId);
            template.setAppkey(appKey);
            template.setStyle(style);

            SingleMessage message = new SingleMessage();
            message.setOffline(true);
            // 离线有效时间,单位为毫秒
            message.setOfflineExpireTime(24 * 3600 * 1000);
            message.setData(template);
            // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
            message.setPushNetWorkType(0);

            Target target = new Target();
            target.setAppId(appId);
            target.setClientId(appPush.getClientId());

            IPushResult ret = null;
            try {
                ret = push.pushMessageToSingle(message, target);
            } catch (RequestException e) {
                System.out.println(e.getMessage());
                ret = push.pushMessageToSingle(message, target, e.getRequestId());
            }
            if (ret != null) {
                System.out.println("返回结果=====" + ret.getResponse().toString());
            } else {
                System.out.println("服务器响应异常=====");
            }
            return AjaxResult.success(ret.getResponse().toString());
        } else {
            return AjaxResult.error("秘钥或参数错误");
        }
    }

3. Test preview

        The official app must be packaged and installed on the mobile phone, and the operation of the first step ( ) must be ensured. The mobile phone uses plus.push.getClientInfo(); to obtain the CID client ID, which is also the ID string that needs to be stored in the database in the next step, as shown in the figure below :Android包名Android应用签名

// 获取App端cid
let CID= plus.push.getClientInfo();
console.log(CID)

 

4. Mobile terminal processing

        The requirement is for the user to send it to a designated person during the review process, and this person can get a push message notification on the mobile phone. Probably the logic is relatively simple. The App side obtains the cid, judges whether the current person wants to receive the message notification by logging in, saves it in the database of the corresponding person through the interface, and the server sends the corresponding cid to the App side through the integrated push sdk , to complete the interaction. Individual push, batch push, and group push can be changed according to business needs.

App.vue page:

    // #ifdef APP-PLUS
		let timer = false;
		plus.push.addEventListener("click",(msg)=>{
			clearTimeout(timer);
			timer = setTimeout(()=>{
				console.log(1111,msg);
			},1500)
		},false)
		plus.push.addEventListener("receive",(msg)=>{
			if("LocalMSG" == msg.payload){
			}else{
				if(msg.type=='receive'){
					var options = {cover:false,title:msg.title};
					plus.push.createMessage(msg.content, msg.payload, options ); 
				 }  
			}
		},false)
	// #endif

log in page:

let pinf = plus.push.getClientInfo();
let cid = pinf && pinf.clientid || ''; //客户端标识
// 登录根据userId更新存数据库clientId
//发送网络请求
let opts = {url: '/app/*****',method: 'post'}
this.$http.httpTokenRequest(opts,{clientId:cid,userId:res.data.data.userId}).then(res => {
    if(res.data.code == 200){
        //继续执行登录逻辑
	}else{
		uni.showToast({title: '登录异常,请联系管理员。',icon: "none",})
	}
})

recommend

It is recommended to take a look at the Unipush Frequently Asked Questions - DCloud Questions and Answers basically summarizes all common questions, hoping to help you.

Guess you like

Origin blog.csdn.net/weixin_45766955/article/details/126658834