uniapp's unipush Android app information push

The first step is to search and push on the uniapp official website

uni-app official website

Introducing the business and activation process, please be sure to read  the UniPush User Guide carefully - DCloud Q&A

The second step, unipush related links

The activated unipush encapsulates a push SDK, but the Dcloud platform does not have technical contact information. Their technical support is paid, so our push side will also pay attention to the integration situation of unipush users and provide technical assistance.

Here is the complete technical document, please read it carefully:

UniPush Usage GuideUniPush Usage Guide-DCloud Q&A

Unipush FAQUnipush FAQ-DCloud Q&A

Unipush consultation portal: DCloud Q&A

Server-side api interface push call document

Push API-Getui Document Center

You can contact the administrator to join the WeChat technical group

Here comes the point~~~

The third step, front-end recording

For unipush information push, the front-end needs to obtain the cid of the app, and the back-end pushes it to the corresponding app through cid.

Activate UniPush push service

Click here to see how to activate UniPush push service

Android platform supports manufacturer push channel

Note: After activating UniPush, you need to configure the "Manufacturer Push Settings" in the background. After configuring the manufacturer parameters, please be sure to submit the cloud packaging and use the "own certificate" to sign the package; install the cloud-packaged installation package on your phone. , and then obtain the cid for offline manufacturer push testing. You cannot use the cid obtained through the base method for offline manufacturer push testing.

To obtain mobile phone manufacturer push setting information, please refer to: Manufacturer Push Application Creation and Configuration Process

Application icons for desktop icons

How to get CID and task ID

  • When the application is run for the first time after installation, the plus.push.getClientInfo method of the 5+ API should be called to obtain the client ID. If the obtained cid is empty, it means that the client's registration with the push server has not been completed. You can use setTimeout to delay retry. try.
    document.addEventListener('plusready', function(){    
    // 页面加载时触发    
    var pinf = plus.push.getClientInfo();    
    var cid = pinf.clientid;//客户端标识    
    }, false );   
  • If pushed by the server, the result will be returned after the request is successful. The value of [taskid] in the result is the task ID; if pushed on the platform, click Details in [Push Record], and the value of [task ID] will be displayed on the page.

Listen for click events

	// 点击推送信息  离线
		plus.push.addEventListener('click', res => {
			//模板消息
			console.log('push--->click');
		});
		// 接收推送信息  在线
		plus.push.addEventListener('receive', res => {
			console.log('push--->receive');

		});

uniapp Android demo code

app.vue

app.vue
<script>
        //引入推送js
	import plusListener from '@/common/unipush-listener.js';
	export default {
		extends: base,
		onLaunch: function() {
			// 信息推送
			//开启推送
			//#ifdef APP-PLUS  
			plusListener.getClientInfoLoop();// 循环获取cid
			plus.runtime.setBadgeNumber(0); // 角标清空
			plusListener.pushListener(); // 监听通知栏信息
			//  #endif 
               }
          } 
</script>

unipush-listener.js 

unipush-listener.js 

/**
 * 监听push 消息 以及后台数据恢复
 */
import store from '@/store'
let timer = false;
var numloop = 0;

export default {
	// 开启监听推送 
	pushListener() {

		console.log('pushListener=================pushListener');
		// 点击推送信息  离线
		plus.push.addEventListener('click', res => {
			//模板消息
			console.log('push--->click');
			this.addEventListenerPushMessage(res, 'click');
		});
		// 接收推送信息  在线
		plus.push.addEventListener('receive', res => {
			console.log('push--->receive');
			this.addEventListenerPushMessage(res, 'receive');

		});
	},
	// 点击click方法,获取push数据,判断是否登录,进行跳转
	addEventListenerPushMessage(push, type) {

		console.log("push, type", push, type);
		clearTimeout(timer);
		timer = setTimeout(function() {
			var row = push.payload;
			console.log(JSON.stringify(row), "JSON.stringify(row)");
			if (store.state.user.token) {
				if (row.id) {
					uni.navigateTo({
						url: '/packagehome/pages/task/TodoList?row=' + JSON.stringify(row)
					})
				} else {
					uni.reLaunch({
						url: '../index/index',
					});
				}
			} else {
				uni.reLaunch({
					url: '/pages/login/login'
				});
			}
		}, 1500)
	},
	// 循环获取clientid信息,直到获取到为止
	getClientInfoLoop() {

		plus.push.getClientInfoAsync(function(info) {
			console.log("info", JSON.stringify(info));

			console.log(info);
			console.log(info.clientid);
			console.log(!info || !info.clientid, (info && info.clientid ? true : false));
			// 如果info不存在,或者info存在,cid不存在则再次获取cid
			if (!info || !info.clientid) {
				console.log("cid为空=========================================");
				let infoTimer = false;
				infoTimer = setInterval(function() {
					console.log('setInterval======');
					console.log('numloop======', numloop++);
					// 同步获取cid
					var pinf = plus.push.getClientInfo();
					var cid = pinf.clientid; //客户端标识 
					console.log(pinf, cid, cid ? true : false);
					if (cid) {
						clearInterval(infoTimer); //清定时器
						uni.setStorageSync('cid', cid); // 储存cid
						console.log(uni.getStorageSync('cid'), '储存的cid======');
						// this.passCid(cid); // 向后台传送cid
						// 是否有cid,登录的时候上传后台,否则首页上传
						var havecid = uni.getStorageSync('havecid');
						console.log('havecid', havecid, cid);
						console.log(!havecid && cid)
						if (!havecid && cid) {
							this.$http.get('/sys/updateCid?clientId=' + cid).then(({
								data
							}) => {
								console.log(cid, 'passCid,向后台传cid========2data', data);
								if (data.success) {
									uni.setStorageSync('havecid', true);
								}
							})

						}
					}
				}, 100);
			} else if (info && info.clientid) {
				// 储存cid
				var cid = info.clientid;
				uni.setStorageSync('cid', cid);
				console.log(uni.getStorageSync('cid'), '本地储存cid======');
				// this.passCid(cid); // 向后台传送cid
				// 是否有cid,登录的时候上传后台,否则首页上传
				var havecid = uni.getStorageSync('havecid');
				console.log('havecid', havecid, cid);
				console.log(!havecid && cid)
				if (!havecid && cid) {
					this.$http.get('/sys/updateCid?clientId=' + cid).then(({
						data
					}) => {
						console.log(cid, 'passCid,向后台传cid========2data', data);
						if (data.success) {
							uni.setStorageSync('havecid', true);
						}
					})

				}
			}

		}, function(e) {
			console.log('Failed', JSON.stringify(e));
			// 同步获取cid
			var pinf = plus.push.getClientInfo();
			var cid = pinf.clientid; //客户端标识 
			if (cid) {
				// 储存cid
				uni.setStorageSync('cid', cid);
				console.log(uni.getStorageSync('cid'), '======');
				this.passCid(cid); // 向后台传送cid
			}
		})
	},
	// 向后台传送cid
	passCid(cid) {

		// 是否有cid,登录的时候上传后台,否则首页上传
		var havecid = uni.getStorageSync('havecid');
		console.log('havecid', havecid, cid, !havecid && cid);
		if (!havecid && cid) {
			this.$http.get('/sys/updateCid?clientId=' + cid).then(({
				data
			}) => {
				console.log(cid, 'passCid,向后台传cid========2data', data);
				if (data.success) {
					uni.setStorageSync('havecid', true);
				}
			})

		}
	},
	// // receive,接收到推送信息,前端创建推送信息
	// addEventListenerReceive(push, type) {

	// 	console.log("push, type", push, type);
	// 	clearTimeout(timer);
	// 	timer = setTimeout(function() {
	// 		plus.push.createMessage(push.content,push.payload,push.options);
	// 	}, 1500)
	// },
}

The following is an example pushed by the Java-sdk package Rest-V2 manufacturer:

    {  
    "request_id":"请填写10到32位的id",  
    "audience": {  
        "cid": [  
            "请填写clientid"  
        ]  
    },  
    "settings":{  
        "ttl":3600000,  
        "strategy":{  
            "default":1  
        }  
    },  
    "push_message":{  
         "transmission":" {title:"标题",content:"内容",payload:"自定义数据"}"  
    },  
    "push_channel":{  
        "android":{  
            "ups":{  
                "notification":{  
                    "title":"安卓离线展示的标题",  
                    "body":"安卓离线展示的标题",  
                    "click_type":"intent",  
                    "intent":"intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;component=io.dcloud.HBuilder/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=测试标题;S.content=测试内容;S.payload=test;end"  
                }  
            }  
        },  
      "ios":{  
            "type":"notify",  
            "payload":"自定义消息",  
            "aps":{  
                "alert":{  
                    "title":"苹果离线展示的标题",  
                    "body":"苹果离线展示的内容"  
                },  
                "content-available":0  
            },  
            "auto_badge":"+1"  
        }  
}

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

Pitfalls encountered

Guess you like

Origin blog.csdn.net/qq_42351675/article/details/123209796