Optimize API

- setTags
- setAlias
- setTagsAndAlias
This commit is contained in:
Hevin 2017-05-26 16:17:54 +08:00
parent 3c88e87b17
commit 9d3d32d725
3 changed files with 33 additions and 26 deletions

View File

@ -161,9 +161,11 @@ JPush SDK 会以广播的形式发送 RegistrationID 到应用程序。
#### 接口定义 #### 接口定义
JPushPlugin.prototype.setTagsWithAlias(tags, alias) ```js
JPushPlugin.prototype.setTags(tags) JPushPlugin.prototype.setTagsWithAlias(tags, alias, successCallback, errorCallback)
JPushPlugin.prototype.setAlias(alias) JPushPlugin.prototype.setTags(tags, successCallback, errorCallback)
JPushPlugin.prototype.setAlias(alias, successCallback, errorCallback)
```
#### 参数说明 #### 参数说明
* tags: * tags:
@ -181,17 +183,15 @@ JPush SDK 会以广播的形式发送 RegistrationID 到应用程序。
* 有效的别名组成:字母(区分大小写)、数字、下划线、汉字。 * 有效的别名组成:字母(区分大小写)、数字、下划线、汉字。
* 限制alias 命名长度限制为 40 字节(判断长度需采用 UTF-8 编码)。 * 限制alias 命名长度限制为 40 字节(判断长度需采用 UTF-8 编码)。
#### 返回值说明 #### 代码示例
函数本身无返回值,但需要注册 `jpush.setTagsWithAlias` 事件来监听设置结果: ```js
window.plugins.jPushPlugin.setTagsWithAlias([tag1, tag2], alias1, function () {
document.addEventListener("jpush.setTagsWithAlias", function(event) { // success callback.
console.log("onTagsWithAlias") }, function (errorMsg) {
var result = "result code:" + event.resultCode + " " // errorMsg 格式为 'errorCode: error message'.
result += "tags:" + event.tags + " " })
result += "alias:" + event.alias + " " ```
$("#tagAliasResult").html(result)
}, false)
#### 错误码定义 #### 错误码定义

View File

@ -116,7 +116,10 @@
if (tag3 != "") { if (tag3 != "") {
tags.push(tag3); tags.push(tag3);
} }
window.plugins.jPushPlugin.setTagsWithAlias(tags, alias); window.plugins.jPushPlugin.setTagsWithAlias(tags, alias, function () {
// Success callback
console.log(tags + ' - ' + alias)
});
} catch (exception) { } catch (exception) {
console.log(exception); console.log(exception);
} }

View File

@ -17,8 +17,12 @@ JPushPlugin.prototype.errorCallback = function (msg) {
console.log('JPush Callback Error: ' + msg) console.log('JPush Callback Error: ' + msg)
} }
JPushPlugin.prototype.callNative = function (name, args, successCallback) { JPushPlugin.prototype.callNative = function (name, args, successCallback, errorCallback) {
cordova.exec(successCallback, this.errorCallback, 'JPushPlugin', name, args) if (errorCallback) {
cordova.exec(successCallback, errorCallback, 'JPushPlugin', name, args)
} else {
cordova.exec(successCallback, this.errorCallback, 'JPushPlugin', name, args)
}
} }
// Common methods // Common methods
@ -66,7 +70,7 @@ JPushPlugin.prototype.clearLocalNotifications = function () {
} }
} }
JPushPlugin.prototype.setTagsWithAlias = function (tags, alias) { JPushPlugin.prototype.setTagsWithAlias = function (tags, alias, successCallback, errorCallback) {
if (tags == null) { if (tags == null) {
this.setAlias(alias) this.setAlias(alias)
return return
@ -77,15 +81,15 @@ JPushPlugin.prototype.setTagsWithAlias = function (tags, alias) {
} }
var arrayTagWithAlias = [tags] var arrayTagWithAlias = [tags]
arrayTagWithAlias.unshift(alias) arrayTagWithAlias.unshift(alias)
this.callNative('setTagsWithAlias', arrayTagWithAlias, null) this.callNative('setTagsWithAlias', arrayTagWithAlias, successCallback, errorCallback)
} }
JPushPlugin.prototype.setTags = function (tags) { JPushPlugin.prototype.setTags = function (tags, successCallback, errorCallback) {
this.callNative('setTags', tags, null) this.callNative('setTags', tags, successCallback, errorCallback)
} }
JPushPlugin.prototype.setAlias = function (alias) { JPushPlugin.prototype.setAlias = function (alias, successCallback, errorCallback) {
this.callNative('setAlias', [alias], null) this.callNative('setAlias', [alias], successCallback, errorCallback)
} }
// 判断系统设置中是否对本应用启用通知。 // 判断系统设置中是否对本应用启用通知。
@ -205,13 +209,13 @@ JPushPlugin.prototype.addNotificationActions = function (actions, categoryId) {
// Android methods // Android methods
JPushPlugin.prototype.setBasicPushNotificationBuilder = function () { JPushPlugin.prototype.setBasicPushNotificationBuilder = function () {
if (device.platform == 'Android') { if (device.platform === 'Android') {
this.callNative('setBasicPushNotificationBuilder', [], null) this.callNative('setBasicPushNotificationBuilder', [], null)
} }
} }
JPushPlugin.prototype.setCustomPushNotificationBuilder = function () { JPushPlugin.prototype.setCustomPushNotificationBuilder = function () {
if (device.platform == 'Android') { if (device.platform === 'Android') {
this.callNative('setCustomPushNotificationBuilder', [], null) this.callNative('setCustomPushNotificationBuilder', [], null)
} }
} }
@ -258,14 +262,14 @@ JPushPlugin.prototype.clearNotificationById = function (id) {
} }
JPushPlugin.prototype.setLatestNotificationNum = function (num) { JPushPlugin.prototype.setLatestNotificationNum = function (num) {
if (device.platform == 'Android') { if (device.platform === 'Android') {
this.callNative('setLatestNotificationNum', [num], null) this.callNative('setLatestNotificationNum', [num], null)
} }
} }
JPushPlugin.prototype.addLocalNotification = function (builderId, content, title, JPushPlugin.prototype.addLocalNotification = function (builderId, content, title,
notificationID, broadcastTime, extras) { notificationID, broadcastTime, extras) {
if (device.platform == 'Android') { if (device.platform === 'Android') {
this.callNative('addLocalNotification', this.callNative('addLocalNotification',
[builderId, content, title, notificationID, broadcastTime, extras], null) [builderId, content, title, notificationID, broadcastTime, extras], null)
} }