uniapp adds Aurora Push

1. Download the Jiguang SDK plug-in from the dcloud plug-in market (it can be packaged in the cloud or offline)

 Aurora JCore official SDK download address

https://ext.dcloud.net.cn/plugin?id=4028https://ext.dcloud.net.cn/plugin?id=4028

Aurora JPush official SDK download address

Aurora JPush official SDK - DCloud plugin market Aurora JPush official SDK HBuilder plugin version https://ext.dcloud.net.cn/plugin?id=4035

Download the SDK plugin 

Cloud packaging: Click for to pack cloud packages from the plug-in market,

Offline packaging: click offline packaging, download the SDK to the local, create a new [nativeplugins] folder in the root directory, and put the downloaded files in it

 2. Red configure manifest.json in the project

(1) For local plug-ins, click [APP native plug-in configuration] in manifest.json, select local plug-ins, click the OK button to import, and configure the application package name and AppKey in the source code view of manifest.json

Add local plugin

 Configure JG-JPUSH, set the application package name of Android and ios

android_package_name: "package name" 

ios_bundle_id: "package name"

Configure JG-JCore, set the application package name of Android and ios and the AppKey of Jiguang official website information

android_package_name: "package name"

ios_bundle_id: "package name"

JPUSH_APPKEY_ANDROID:"AppKey"

(2) Cloud plug-in configuration

Click [APP native plug-in configuration] in manifest.json, select the cloud plug-in, click the OK button to import, and configure the application package name and AppKey in the source code view of manifest.json (consistent with the above)

3. Page code

 Add the following code in App.vue

<script>
const jpushModule = uni.requireNativePlugin('JG-JPush')
	export default {
		onLaunch: function() {
			console.log('App Launch')
			if (uni.getSystemInfoSync().platform == "ios") {
				// 请求定位权限
				let locationServicesEnabled = jpushModule.locationServicesEnabled()
				let locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
				console.log('locationAuthorizationStatus', locationAuthorizationStatus)
				if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
					jpushModule.requestLocationAuthorization((result) => {
						console.log('定位权限', result.status)
					})
				}
				jpushModule.requestNotificationAuthorization((result) => {
					let status = result.status
					if (status < 2) {
						uni.showToast({
							icon: 'none',
							title: '您还没有打开通知权限',
							duration: 3000
						})
					}
				})
			}

			jpushModule.initJPushService();
			jpushModule.setLoggerEnable(true);
			jpushModule.addConnectEventListener(result => {
				console.log(result)
				let connectEnable = result.connectEnable
				uni.$emit('connectStatusChange', connectEnable)
			});


			jpushModule.addNotificationListener(result => {
				console.log(result)
				if (result.notificationEventType == "notificationOpened") {
				  //点击窗口通知栏推送的消息 跳转指定页面
					uni.navigateTo({
						url: '/pages/home/warningCenter/warningCenter',
						complete(res) {
							console.log(res);
						}
					})
				}

			});

			jpushModule.addCustomMessageListener(result => {
				let type = result.type
				let messageType = result.messageType
				let content = result.content
				uni.showToast({
					icon: 'none',
					title: JSON.stringify(result),
					duration: 3000
				})
			})

			jpushModule.addLocalNotificationListener(result => {
				let messageID = result.messageID
				let title = result.title
				let content = result.content
				let extras = result.extras
				uni.showToast({
					icon: 'none',
					title: JSON.stringify(result),
					duration: 3000
				})
			})

			jpushModule.addGeofenceListener(result => {
				let code = result.code
				let type = result.type
				let geofenceId = result.geofenceId
				let userInfo = result.userInfo
				uni.showToast({
					icon: 'none',
					title: '触发地理围栏',
					duration: 3000
				})
			})

			jpushModule.setIsAllowedInMessagePop(true)
			jpushModule.pullInMessage(result => {
				let code = result.code
				console.log(code)
			})

			jpushModule.addInMessageListener(result => {
				let eventType = result.eventType
				let messageType = result.messageType
				let content = result.content
				console.log('inMessageListener', eventType, messageType, content)

				uni.showToast({
					icon: 'none',
					title: JSON.stringify(result),
					duration: 3000
				})
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

login page code

<script>
	const jpushModule = uni.requireNativePlugin('JG-JPush')
	export default {
		data() {
			return {
				//极光推送
				connectStatus: '未连接',
				registrationID: '未获得',
			}
		},
		methods: {
			//点击登录按钮
			login() {
              //登录成功后
                 //设置别名
				jpushModule.setAlias({
						'alias': this.phone,
						'sequence': 1
						})
			},
			connect() {
				uni.$on('connectStatusChange', (connectStatus) => {
					console.log('进入连接')
					var connectStr = ''
					if (connectStatus == true) {
						connectStr = '已连接'
						this.getRegistrationID()
					} else {
						connectStr = '未连接'
					}
					console.log('监听到了连接状态变化 --- ', connectStr)
					this.connectStatus = connectStr
				})
			},
			//获取推送ID
			getRegistrationID() {
				jpushModule.getRegistrationID(result => {
					let registerID = result.registerID
					console.log(result)
					this.registrationID = registerID
				})
			}
		},
		onLoad() {
			this.connect()
		}
	}
</script>

Guess you like

Origin blog.csdn.net/workhard0905/article/details/127109799