unipush 2.0 process and pitfall records (back-end calling interface, front-end push)

unipush online and offline examples

Create unipush application inDCLOUD Developer Center

If you encounter that the Android application signature is not automatically generated after selecting the Android package name, it will look like the picture below.

This situation is most likely caused by directly creating a cloud certificate without editing the application information.

If you don’t have a cloud certificate, look hereHere

In My Applications, find the application that requires unipush, click Edit, and then fill in the contents of the cloud certificate.

For the cloud service space, just follow the instructions. This is not my fault. After linking, click Activate

Find the project in hbuilder, right-click the project name, and create a cloud function

You can read this stepOfficial Documents, which is quite detailed. Be sure to write this step carefully, and then there will be a callback when the backend requests the interface

Add push module and set targetSdkersion (targetSdkersion is too high and some phones cannot run it)

Find the push module you just added and set up the cloud function (the following is a screenshot of my code, the above is the official screenshot, so the file names are different, but the content is the same)

'use strict';
const uniPush = uniCloud.getPushManager({
	appId: "__UNI__A4C6D3499"
})
exports.main = async (event) => {
	let obj = JSON.parse(event.body)
	const res = await uniPush.sendMessage({
		"push_clientid": obj.cids, // 设备id,支持多个以数组的形式指定多个设备,如["cid-1","cid-2"],数组长度不大于1000  
		"title": obj.title, // 标题  
		"content": obj.content, // 内容  
		"payload": obj.data, // 数据  
		"force_notification": true, // 服务端推送 需要加这一句  
		"request_id": obj.request_id //请求唯一标识号,10-32位之间;如果request_id重复,会导致消息丢失   
	})
	return res   //一定要return回去
};

 Final upload and deployment

Write these two lines of code in app.vue, one is the event of clicking the push message, and the other is to obtain the user cid information (used to push to the specified user, such as chat information, order information, all for the specified user ). Then be sure to check the system permissions first and copy the code of this boss directly. Some mobile phones will automatically remind you whether to receive message notifications for the first time. You can make this compatible by yourself. Customizing the base requires manually turning on this permission. Link address

// #ifdef APP-PLUS
			plus.push.addEventListener("click", function(msg) {
				console.log(msg);
				uni.switchTab({
					url:'/pages/user/index'
				})
			}, false);
			uni.getPushClientId({
					success: res => {
						console.log(res.cid);
					}
				})
				this.getQuanxian()
			// #endif

Another way to obtain notification permission isOfficial Document

// #ifdef APP-PLUS 
			const notificationAuthorized = uni.getAppAuthorizeSetting().notificationAuthorized
			if(notificationAuthorized=='denied'){
				uni.showModal({
					title: '提示',
					content: '是否前往打开通知权限',
					success: res => {
						if (res.confirm) {
							this.openTongZhi()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
			// that.getQuanxian()
			// #endif 

Then check the configuration file (if you want to configure offline, please read the offline notification title part first)

Some mobile phones fail to run custom bases. Try setting this to 28.

Open the custom base package and run

Then the package must be a custom base for local debugging.

Detection base

Before running the custom dock, you must first uninstall all related applications in the phone, and then get cid to test it. It will be good if you can get the information.

Configure cloud function url (the function is to directly adjust the interface on the backend and then push it)

This parameter has a one-to-one correspondence! ! !

The device uses this interface both online and offline! ! !

Offline custom ringtones

When I did it, Xiaomi didn't support it, and it was mainly for offline notifications to have sounds.

Custom push ringtones and channels - DCloud plug-in market

Use after importing the plug-in

channelId is

If there is no sound and soundName is set to "", as long as you import this and there is a system ringtone, just accept it.

Offline notification (check this if the app needs to be online)

Profile search

Go directly to each manufacturer's platform,Notes on the picture aboveThere are manufacturer addresses there. SearchMessage push is just a matter of activation. We are the first to launch it on most platforms. Huawei can apply directly without going online.

Like Huawei, you can just find the corresponding fields and fill them in.

Summary and pitfalls encountered

0. In dcloud, when creating unipush2.0, found the Android package name but did not automatically generate the Android application signature.

Click to modify and improve it

1. First check unipush2.0 to disable offline push, and then get the cid in app.vue for testing. Then create the cloud function.

2.The custom dock cannot be run:Uninstall all related packages on the phone and run it again. Otherwise, targetSdkersion should be set lower.

3.There is no response or error when the backend sends a request:No response may be a problem with the configuration of the uniapp cloud function, or get Either request or post request can be made, but the request body is different. Then the parameters where the cloud function accepts and where the request is sent must be the same

4.Did not receive push message:Check the phone firstMessage notification permission Is it turned on? Then open the app to find the cid obtained through uni.getPushClientId, and then check it in dcloud

See if you can check the device status. If you can't check it online, check which step is wrong, or uninstall the previous app and re-install the custom base package. You can check offline, but if you can't receive the message, check here

5. Modify the configuration file, etc., but then it cannot run. Uninstall it on the phone and re-install the custom package.

6. Some mobile phones have no sound when offline. The configurations of each manufacturer are different. Well, ask a technical supporter about this. They will know when the manufacturer has updated it.

7. The official GeTui document tells you how to integrate various manufacturers (mainly offline push). Link address

Finally: GeTui’s technical customer service is really friendly. If you don’t know something, you can ask them directly.

Guess you like

Origin blog.csdn.net/m0_59203969/article/details/134527003