jpush-phonegap-plugin/www/JPushPlugin.js

345 lines
8.7 KiB
JavaScript
Raw Normal View History

2014-06-05 16:36:54 +08:00
2016-01-21 15:15:48 +08:00
var JPushPlugin = function(){
};
2015-01-16 10:42:06 +08:00
//private plugin function
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.receiveMessage = {}
JPushPlugin.prototype.openNotification = {}
JPushPlugin.prototype.receiveNotification = {}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.isPlatformIOS = function() {
var isPlatformIOS = device.platform == "iPhone"
|| device.platform == "iPad"
|| device.platform == "iPod touch"
|| device.platform == "iOS";
return isPlatformIOS;
2014-06-10 14:54:17 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.error_callback = function(msg) {
console.log("Javascript Callback Error: " + msg);
2014-06-05 16:36:54 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.call_native = function(name, args, callback) {
ret = cordova.exec(callback, this.error_callback, 'JPushPlugin', name, args);
2014-06-10 14:14:02 +08:00
return ret;
2014-06-05 16:36:54 +08:00
}
2016-03-10 13:42:09 +08:00
2015-01-16 10:42:06 +08:00
//public plugin function
2014-06-05 16:36:54 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.startLogPageView = function(pageName) {
if(this.isPlatformIOS()) {
this.call_native("startLogPageView", [pageName], null);
}
2014-06-10 14:14:02 +08:00
}
2014-06-05 16:36:54 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.stopLogPageView = function(pageName) {
if(this.isPlatformIOS()) {
this.call_native("stopLogPageView", [pageName], null);
2014-06-12 14:39:46 +08:00
}
2014-06-05 16:36:54 +08:00
}
2014-06-04 13:20:24 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.beginLogPageView = function(pageName, duration) {
if(this.isPlatformIOS()) {
this.call_native("beginLogPageView", [pageName, duration], null);
2015-01-16 10:42:06 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge) {
if(this.isPlatformIOS()) {
this.call_native("setApplicationIconBadgeNumber", [badge], null);
2015-03-25 16:11:04 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.getApplicationIconBadgeNumber = function(callback) {
if(this.isPlatformIOS()) {
this.call_native("getApplicationIconBadgeNumber", [], callback);
2016-01-21 15:15:48 +08:00
}
2016-03-10 13:42:09 +08:00
}
2015-01-16 10:42:06 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setTagsWithAlias = function(tags, alias) {
try {
if(tags == null) {
this.setAlias(alias);
return;
}
if(alias == null) {
this.setTags(tags);
2014-06-12 11:02:33 +08:00
return;
}
2016-03-10 13:42:09 +08:00
var arrayTagWithAlias = [tags];
2014-06-12 11:02:33 +08:00
arrayTagWithAlias.unshift(alias);
2016-03-10 13:42:09 +08:00
this.call_native("setTagsWithAlias", arrayTagWithAlias, null);
} catch(exception) {
2014-06-12 11:02:33 +08:00
console.log(exception);
}
2014-01-20 18:27:31 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setTags = function(tags) {
try {
this.call_native("setTags", tags, null);
} catch(exception) {
2014-06-12 11:02:33 +08:00
console.log(exception);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setAlias = function(alias) {
try {
this.call_native("setAlias", [alias], null);
} catch(exception) {
2014-06-10 14:14:02 +08:00
console.log(exception);
}
2014-01-20 18:27:31 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.getRegistrationID = function(callback) {
try {
var data = [];
this.call_native("getRegistrationID", [data], callback);
} catch(exception) {
2014-06-10 14:14:02 +08:00
console.log(exception);
}
2013-10-18 11:14:33 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setBadge = function(value) {
if(this.isPlatformIOS()) {
try {
this.call_native("setBadge", [value], null);
} catch(exception) {
console.log(exception);
}
}
2014-09-24 20:02:07 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.resetBadge = function() {
if(this.isPlatformIOS()) {
try {
var data = [];
this.call_native("resetBadge", [data], null);
} catch(exception) {
console.log(exception);
}
2014-09-24 20:02:07 +08:00
}
2016-03-10 13:42:09 +08:00
}
2014-09-24 20:02:07 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setDebugModeFromIos = function() {
if(this.isPlatformIOS()) {
var data = [];
this.call_native("setDebugModeFromIos", [data], null);
}
2014-09-24 20:02:07 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setLogOFF = function() {
if(this.isPlatformIOS()) {
var data = [];
this.call_native("setLogOFF", [data], null);
2014-09-24 20:02:07 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setCrashLogON = function() {
if (this.isPlatformIOS()) {
2016-03-10 13:42:09 +08:00
var data = [];
this.call_native("crashLogON", [data], null);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.addLocalNotificationForIOS = function(delayTime, content,
badge, notificationID, extras) {
if (this.isPlatformIOS()) {
2016-03-10 13:42:09 +08:00
var data = [delayTime, content, badge, notificationID, extras];
this.call_native("setLocalNotification", data, null);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.deleteLocalNotificationWithIdentifierKeyInIOS = function(
identifierKey) {
if (this.isPlatformIOS()) {
2016-03-10 13:42:09 +08:00
var data = [identifierKey];
this.call_native("deleteLocalNotificationWithIdentifierKey", data, null);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.clearAllLocalNotifications = function(){
if (this.isPlatformIOS()) {
2016-03-10 13:42:09 +08:00
var data = [];
this.call_native("clearAllLocalNotifications", data, null);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setLocation = function(latitude, longitude){
if (this.isPlatformIOS()) {
2016-03-10 13:42:09 +08:00
var data = [latitude, longitude];
this.call_native("setLocation", data, null);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.receiveMessageIniOSCallback = function(data) {
try {
console.log("JPushPlugin:receiveMessageIniOSCallback--data:" + data);
2014-07-22 18:51:48 +08:00
var bToObj = JSON.parse(data);
var content = bToObj.content;
2016-03-10 13:42:09 +08:00
console.log(content);
} catch(exception) {
console.log("JPushPlugin:receiveMessageIniOSCallback" + exception);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data) {
try {
console.log("JPushPlugin:receiveMessageInAndroidCallback");
2016-03-15 11:06:09 +08:00
data = JSON.stringify(data);
2016-03-10 13:42:09 +08:00
var bToObj = JSON.parse(data);
this.receiveMessage = bToObj
cordova.fireDocumentEvent('jpush.receiveMessage', null);
} catch(exception) {
console.log("JPushPlugin:pushCallback " + exception);
2014-07-23 14:46:02 +08:00
}
}
2015-01-26 14:23:45 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.openNotificationInAndroidCallback = function(data) {
try {
console.log("JPushPlugin:openNotificationInAndroidCallback");
2016-03-15 11:06:09 +08:00
data = JSON.stringify(data);
2016-03-10 13:42:09 +08:00
var bToObj = JSON.parse(data);
this.openNotification = bToObj;
cordova.fireDocumentEvent('jpush.openNotification', null);
} catch(exception) {
console.log(exception);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.receiveNotificationInAndroidCallback = function(data) {
try{
console.log("JPushPlugin:receiveNotificationInAndroidCallback");
2016-03-15 11:06:09 +08:00
data = JSON.stringify(data);
2016-03-10 13:42:09 +08:00
var bToObj = JSON.parse(data);
this.receiveNotification = bToObj;
cordova.fireDocumentEvent('jpush.receiveNotification', null);
} catch(exception) {
2014-06-10 14:14:02 +08:00
console.log(exception);
}
2013-10-21 11:28:42 +08:00
}
2016-03-10 13:42:09 +08:00
2014-06-12 14:39:46 +08:00
//android single
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setBasicPushNotificationBuilder = function() {
2014-06-12 14:39:46 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
data = [];
this.call_native("setBasicPushNotificationBuilder", data, null);
2014-06-12 14:39:46 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setCustomPushNotificationBuilder = function() {
2014-06-12 14:39:46 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
data = [];
this.call_native("setCustomPushNotificationBuilder", data, null);
2014-06-12 14:39:46 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.stopPush = function() {
data = [];
this.call_native("stopPush", data, null);
2014-06-12 14:39:46 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.resumePush = function() {
data = [];
this.call_native("resumePush", data, null);
2014-06-12 14:39:46 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setDebugMode = function(mode) {
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
this.call_native("setDebugMode", [mode], null);
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.clearAllNotification = function() {
2014-06-12 14:39:46 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
data = [];
this.call_native("clearAllNotification", data, null);
2014-06-12 14:39:46 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.clearNotificationById = function(notificationId) {
if(device.platform == "Android") {
data = [];
this.call_native("clearNotificationById", [notificationId], null);
}
2015-08-06 17:11:06 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setLatestNotificationNum = function(num) {
if(device.platform == "Android") {
this.call_native("setLatestNotificationNum", [num], null);
2014-06-12 14:39:46 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.isPushStopped = function(callback) {
data = [];
this.call_native("isPushStopped", data, callback);
2014-06-12 14:39:46 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.init = function() {
if(this.isPlatformIOS()) {
var data = [];
this.call_native("initial", data, null);
} else {
data = [];
this.call_native("init", data, null);
}
2014-06-12 14:39:46 +08:00
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.setDebugMode = function(mode) {
2014-06-12 14:39:46 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
this.call_native("setDebugMode", [mode], null);
2014-06-12 14:39:46 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.addLocalNotification = function(builderId, content, title,
notificaitonID, broadcastTime, extras) {
2015-01-15 10:31:43 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
data = [builderId, content, title, notificaitonID, broadcastTime, extras];
this.call_native("addLocalNotification", data, null);
2015-01-15 10:31:43 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.removeLocalNotification = function(notificationID) {
2015-01-15 10:31:43 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
this.call_native("removeLocalNotification", [notificationID], null);
2015-01-15 10:31:43 +08:00
}
}
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.clearLocalNotifications = function() {
2015-01-15 10:31:43 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
data = [];
this.call_native("clearLocalNotifications", data, null);
2015-01-15 10:31:43 +08:00
}
}
2015-01-26 14:23:45 +08:00
2016-03-10 13:42:09 +08:00
JPushPlugin.prototype.reportNotificationOpened = function(msgID) {
2015-01-26 14:23:45 +08:00
if(device.platform == "Android") {
2016-03-10 13:42:09 +08:00
this.call_native("reportNotificationOpened", [msgID], null);
2015-01-26 14:23:45 +08:00
}
}
2014-06-12 14:39:46 +08:00
/**
*是否开启统计分析功能用于用户使用时长活跃用户用户打开次数的统计并上报到服务器上
* Portal 上展示给开发者
**/
JPushPlugin.prototype.setStatisticsOpen = function(mode) {
2016-03-10 13:42:09 +08:00
if(device.platform == "Android") {
this.call_native("setStatisticsOpen", [mode], null);
}
}
2014-07-22 20:14:01 +08:00
//iOS single
2014-06-10 14:14:02 +08:00
2016-03-10 13:42:09 +08:00
if(!window.plugins) {
2014-06-10 14:14:02 +08:00
window.plugins = {};
2013-10-18 11:14:33 +08:00
}
2014-06-10 14:14:02 +08:00
2016-03-10 13:42:09 +08:00
if(!window.plugins.jPushPlugin) {
2014-06-10 14:14:02 +08:00
window.plugins.jPushPlugin = new JPushPlugin();
}
2016-01-21 15:15:48 +08:00
module.exports = new JPushPlugin();