这两天在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
}
复制代码