uni-app接入極光推送

這兩天在uni-app上接了極光推送,遇到一些坑,網上也沒搜到相關內容,因此就有了這篇文章。javascript

接入

官方接入指南:github.com/jpush/jpush…html

android工程配置參考官方demo,沒有什麼問題。 問題在於這一步vue

拷貝 ./jpush.js 到你 Android Studio 工程的 /assets/apps/HBuilder應用名/js/ 下。java

官方demo裏是直接在html文件內,用script標籤引用的,但因爲咱們使用的uni-app採用的是vue的語法,不能像demo同樣引用。 咱們將jpush.js文件拷貝到工程下面,經過import語法引用,這時會報錯,看了jpush源文件發現裏面使用document、window這種app端不支持的對象,這時咱們就須要修改jpush文件的代碼了。android

  • 去除document 因爲vue文件不須要plus ready、也不會執行plus ready,因此先將plusready的事件移除,直接執行裏面的方法。 jpush本來是經過fireDocumentEvent這個方法派發事件,裏面用到了document.createEvent('HTMLEvents')document.dispatchEvent方法,這裏咱們須要修改派發事件的機制,改成使用 uni.$emit()派發 uni.$on()接收git

  • 去除windowgithub

window.plus.bridge // 修改成plus.bridge
window.plus.Push // 修改成plus.Push
複製代碼

這樣修改以後再引用就能夠正常使用了。json

附上完整代碼app

export function initJPush() {
		var _BARCODE = 'Push' // 插件名稱
		var B = plus.bridge
		var JPushPlugin = {
			receiveMessage: {},
			openNotification: {},
			receiveNotification: {},

			callNative: function(fname, args, successCallback) {
				var callbackId = this.getCallbackId(successCallback, this.errorCallback)
				if (args != null) {
					args.unshift(callbackId)
				} else {
					var args = [callbackId]
				}
				return B.exec(_BARCODE, fname, args)
			},
			getCallbackId: function(successCallback) {
				var success = typeof successCallback !== 'function' ? null : function(args) {
					successCallback(args)
				}
				return B.callbackId(success, this.errorCallback)
			},
			errorCallback: function(errorMsg) {
				console.log('Javascript callback error: ' + errorMsg)
			},
			fireDocumentEvent: function(ename, jsonData) {
				jsonData = JSON.stringify(jsonData)
				var data = JSON.parse(jsonData)
				uni.$emit(ename,data)
			},
			// Common method
			getRegistrationID: function(successCallback) {
				this.callNative('getRegistrationID', null, successCallback)
			},
			setTags: function(tags) {
				this.callNative('setTags', [tags], null)
			},
			setAlias: function(alias) {
				this.callNative('setAlias', [alias], null)
			},
			setTagsWithAlias: function(tags, alias) {
				if (alias == null) {
					this.setTags(tags)
					return
				}
				if (tags == null) {
					this.setAlias(alias)
					return
				}
				var arrayTagWithAlias = [tags]
				arrayTagWithAlias.unshift(alias)
				this.callNative('setTagsWithAlias', arrayTagWithAlias, null)
			},
			stopPush: function() {
				this.callNative('stopPush', null, null)
			},
			resumePush: function() {
				this.callNative('resumePush', null, null)
			},
			isPushStopped: function(successCallback) {
				this.callNative('isPushStopped', null, successCallback)
			},

			// Android methods
			init: function() {
				if (plus.os.name == 'Android') {
					this.callNative('init', null, null)
				}
			},
			setDebugMode: function(mode) {
				if (plus.os.name == 'Android') {
					this.callNative('setDebugMode', [mode], null)
				}
			},
			addLocalNotification: function(builderId, content, title, notiID, broadcastTime, extras) {
				if (plus.os.name == 'Android') {
					data = [builderId, content, title, notiID, broadcastTime, extras]
					this.callNative('addLocalNotification', data, null)
				}
			},
			removeLocalNotification: function(notificationId) {
				if (plus.os.name == 'Android') {
					this.callNative('removeLocalNotification', [notificationId], null)
				}
			},
			clearLocalNotifications: function() {
				if (plus.os.name == 'Android') {
					this.callNative('clearLocalNotifications', null, null)
				}
			},
			clearAllNotification: function() {
				if (plus.os.name == 'Android') {
					this.callNative('clearAllNotification', null, null)
				}
			},
			clearNotificationById: function(notificationId) {
				if (plus.os.name == 'Android') {
					this.callNative('clearNotificationById', [notificationId], null)
				}
			},
			setBasicPushNotificationBuilder: function() {
				if (plus.os.name == 'Android') {
					this.callNative('setBasicPushNotification', null, null)
				}
			},
			setCustomPushNotificationBuilder: function() {
				if (plus.os.name == 'Android') {
					this.callNative('setCustomPushNotificationBuilder', null, null)
				}
			},
			setLatestNotificationNum: function(num) {
				if (plus.os.name == 'Android') {
					this.callNative('setLatestNotificationNum', [num], null)
				}
			},
			setPushTime: function(successCallback, weekDays, startHour, endHour) {
				if (plus.os.name == 'Android') {
					this.callNative('setPushTime', [weekDays, startHour, endHour], successCallback)
				}
			},
			setSilenceTime: function(successCallback, startHour, startMinute, endHour, endMinute) {
				if (plus.os.name == 'Android') {
					this.callNative('setSilenceTime', [startHour, startMinute, endHour, endMinute], successCallback)
				}
			},
			requestPermission: function() {
				if (plus.os.name == 'Android') {
					this.callNative('requestPermission', null, null)
				}
			},
			getConnectionState: function(successCallback) {
				if (plus.os.name == 'Android') {
					this.callNative('getConnectionState', null, successCallback)
				}
			},
			onGetRegistrationId: function(rId) {
				if (plus.os.name == 'Android') {
					this.fireDocumentEvent('jpush.onGetRegistrationId', rId)
				}
			},
			getLaunchAppCacheNotification: function(successCallback) {
				this.callNative('getLaunchAppCacheNotification', null, successCallback)
			},
			clearLaunchAppCacheNotification: function() {
				if (plus.os.name == 'Android') {
					this.callNative('clearLaunchAppCacheNotification', null, null)
				}
			},
			receiveMessageInAndroidCallback: function(data) {
				if (plus.os.name == 'Android') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.receiveMessage = jsonObj
					this.fireDocumentEvent('jpush.receiveMessage', this.receiveMessage)
				}
			},
			openNotificationInAndroidCallback: function(data) {
				if (plus.os.name == 'Android') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.openNotification = jsonObj
					this.fireDocumentEvent('jpush.openNotification', this.openNotification)
				}
			},
			openNotificationIniOSCallback: function(data) {
				if (plus.os.name == 'iOS') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.openNotification = jsonObj
					this.fireDocumentEvent('jpush.openNotification', this.openNotification)
				}
			},
			receiveNotificationInAndroidCallback: function(data) {
				if (plus.os.name == 'Android') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.receiveNotification = jsonObj
					this.fireDocumentEvent('jpush.receiveNotification', this.receiveNotification)
				}
			},
			receiveNotificationIniOSCallback: function(data) {
				if (plus.os.name == 'iOS') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.receiveNotification = jsonObj

					this.fireDocumentEvent('jpush.receiveNotification', this.receiveNotification)
				}
			},
			receiveMessageIniOSCallback: function(data) {
				if (plus.os.name == 'iOS') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.receiveMessage = jsonObj
					this.fireDocumentEvent('jpush.receiveMessage', this.receiveMessage)
				}
			},
			receiveNotificationLaunceAppIniOSCallback: function(data) {
				if (plus.os.name == 'iOS') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.receiveMessage = jsonObj
					this.fireDocumentEvent('jpush.receiveNotificationLaunchApp', this.receiveMessage)
				}
			},
			receiveNotificationBackgroundIniOSCallback: function(data) {
				if (plus.os.name == 'iOS') {
					data = JSON.stringify(data)
					var jsonObj = JSON.parse(data)
					this.receiveMessage = jsonObj
					this.fireDocumentEvent('jpush.receiveNotificationBackground', this.receiveMessage)
				}
			},
			// iOS methods
			setBadge: function(value) {
				if (plus.os.name == 'iOS') {
					try {
						this.callNative('setBadge', [value], null)
					} catch (exception) {
						console.log(exception)
					}
				}
			},
			resetBadge: function() {
				if (plus.os.name == 'iOS') {
					try {
						this.callNative('resetBadge', [], null)
					} catch (exception) {
						console.log(exception)
					}
				}
			},
			setApplicationIconBadgeNumber: function(badge) {
				if (plus.os.name == 'iOS') {
					this.callNative('setApplicationIconBadgeNumber', [badge], null)
				}
			},
			getApplicationIconBadgeNumber: function(callback) {
				if (plus.os.name == 'iOS') {
					this.callNative('getApplicationIconBadgeNumber', [], callback)
				}
			},
			startLogPageView: function(pageName) {
				if (plus.os.name == 'iOS') {
					this.callNative('startLogPageView', [pageName], null)
				}
			},
			stopLogPageView: function(pageName) {
				if (plus.os.name == 'iOS') {
					this.callNative('stopLogPageView', [pageName], null)
				}
			},
			beginLogPageView: function(pageName, duration) {
				if (plus.os.name == 'iOS') {
					this.callNative('beginLogPageView', [pageName, duration], null)
				}
			},
			setLogOFF: function() {
				if (plus.os.name == 'iOS') {
					this.callNative('setLogOFF', [], null)
				}
			},
			setCrashLogON: function() {
				if (plus.os.name == 'iOS') {
					this.callNative('crashLogON', [], null)
				}
			},
			setLocation: function(latitude, longitude) {
				if (plus.os.name == 'iOS') {
					this.callNative('setLocation', [latitude, longitude], null)
				}
			},
			addLocalNotificationIniOS: function(delayTime, content, badge, notificationID, extras) {
				if (plus.os.name == 'iOS') {
					var data = [delayTime, content, badge, notificationID, extras]
					this.call_native('setLocalNotification', data, null)
				}
			},
			deleteLocalNotificationWithIdentifierKeyIniOS: function(identifierKey) {
				if (plus.os.name == 'iOS') {
					var data = [identifierKey]
					this.call_native('deleteLocalNotificationWithIdentifierKey', data, null)
				}
			},
			clearAllLocalNotificationsIniOS: function() {
				if (plus.os.name == 'iOS') {
					this.call_native('clearAllLocalNotifications', [], null)
				}
			}
		}

		JPushPlugin.init()
		plus.Push = JPushPlugin
}

複製代碼
相關文章
相關標籤/搜索