解决当应用在后台时无法获取通知标题的 bug
This commit is contained in:
Hevin 2016-04-15 16:17:33 +08:00
parent ffe2fee88d
commit 2783be37af
2 changed files with 160 additions and 108 deletions

View File

@ -37,30 +37,32 @@ import cn.jpush.android.data.JPushLocalNotification;
public class JPushPlugin extends CordovaPlugin { public class JPushPlugin extends CordovaPlugin {
private final static List<String> methodList = private final static List<String> methodList =
Arrays.asList( Arrays.asList(
"getRegistrationID", "addLocalNotification",
"setTags", "clearAllNotification",
"setTagsWithAlias", "clearLocalNotifications",
"setAlias", "clearNotificationById",
"getNotification", "getNotification",
"setBasicPushNotificationBuilder", "getRegistrationID",
"setCustomPushNotificationBuilder", "init",
"setPushTime", "isPushStopped",
"init", "onPause",
"setDebugMode", "onResume",
"stopPush", "requestPermission",
"resumePush", "removeLocalNotification",
"isPushStopped", "reportNotificationOpened",
"setLatestNotificationNum", "resumePush",
"setPushTime", "setAlias",
"clearAllNotification", "setBasicPushNotificationBuilder",
"clearNotificationById", "setCustomPushNotificationBuilder",
"addLocalNotification", "setDebugMode",
"removeLocalNotification", "setLatestNotificationNum",
"clearLocalNotifications", "setPushTime",
"onResume", "setTags",
"onPause", "setTagsWithAlias",
"reportNotificationOpened", "setSilenceTime",
"setStatisticsOpen"); "setStatisticsOpen",
"stopPush"
);
private ExecutorService threadPool = Executors.newFixedThreadPool(1); private ExecutorService threadPool = Executors.newFixedThreadPool(1);
private static JPushPlugin instance; private static JPushPlugin instance;
@ -95,7 +97,7 @@ public class JPushPlugin extends CordovaPlugin {
//这样做是为了和 iOS 统一 //这样做是为了和 iOS 统一
if (openNotificationAlert != null) { if (openNotificationAlert != null) {
notificationAlert = null; notificationAlert = null;
transmitNotificationOpen(notificationTitle, openNotificationAlert, transmitNotificationOpen(openNotificationTitle, openNotificationAlert,
openNotificationExtras); openNotificationExtras);
} }
if (notificationAlert != null) { if (notificationAlert != null) {
@ -121,7 +123,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
if (openNotificationAlert != null) { if (openNotificationAlert != null) {
notificationAlert = null; notificationAlert = null;
transmitNotificationOpen(notificationTitle, openNotificationAlert, transmitNotificationOpen(openNotificationTitle, openNotificationAlert,
openNotificationExtras); openNotificationExtras);
} }
if (notificationAlert != null) { if (notificationAlert != null) {
@ -209,9 +211,6 @@ public class JPushPlugin extends CordovaPlugin {
if (instance == null) { if (instance == null) {
return; return;
} }
if (JPushPlugin.shouldCacheMsg) {
return;
}
JSONObject data = getNotificationObject(title, alert, extras); JSONObject data = getNotificationObject(title, alert, extras);
String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);"; String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);";
final String js = String.format(format, data.toString()); final String js = String.format(format, data.toString());
@ -503,6 +502,48 @@ public class JPushPlugin extends CordovaPlugin {
} }
} }
/**
* 设置通知静默时间
* http://docs.jpush.io/client/android_api/#api_5
*/
void setSilenceTime(JSONArray data, CallbackContext callbackContext) {
try {
int startHour = data.getInt(0);
int startMinute = data.getInt(1);
int endHour = data.getInt(2);
int endMinute = data.getInt(3);
if (!isValidHour(startHour) || !isValidMinute(startMinute)) {
callbackContext.error("开始时间数值错误");
return;
}
if(!isValidHour(endHour) || !isValidMinute(endMinute)) {
callbackContext.error("结束时间数值错误");
return;
}
JPushInterface.setSilenceTime(cordovaActivity, startHour, startMinute,
endHour, endMinute);
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("error: reading json data.");
}
}
private boolean isValidHour(int hour) {
return !(hour < 0 || hour > 23);
}
private boolean isValidMinute(int minute) {
return !(minute < 0 || minute > 59);
}
/**
* 用于 Android 6.0 以上系统申请权限具体可参考
* http://docs.Push.io/client/android_api/#android-60
*/
void requestPermission(JSONArray data, CallbackContext callbackContext) {
JPushInterface.requestPermission(cordovaActivity);
}
private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() { private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() {
@Override @Override
public void gotResult(int code, String alias, Set<String> tags) { public void gotResult(int code, String alias, Set<String> tags) {

View File

@ -27,38 +27,42 @@ JPushPlugin.prototype.call_native = function(name, args, callback) {
return ret; return ret;
} }
//public plugin function // public methods
JPushPlugin.prototype.init = function() {
JPushPlugin.prototype.startLogPageView = function(pageName) {
if(this.isPlatformIOS()) { if(this.isPlatformIOS()) {
this.call_native("startLogPageView", [pageName], null); var data = [];
} this.call_native("initial", data, null);
} } else {
data = [];
JPushPlugin.prototype.stopLogPageView = function(pageName) { this.call_native("init", data, null);
if(this.isPlatformIOS()) {
this.call_native("stopLogPageView", [pageName], null);
} }
} }
JPushPlugin.prototype.beginLogPageView = function(pageName, duration) { JPushPlugin.prototype.getRegistrationID = function(callback) {
if(this.isPlatformIOS()) { try {
this.call_native("beginLogPageView", [pageName, duration], null); var data = [];
this.call_native("getRegistrationID", [data], callback);
} catch(exception) {
console.log(exception);
} }
} }
JPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge) { JPushPlugin.prototype.stopPush = function() {
if(this.isPlatformIOS()) { data = [];
this.call_native("setApplicationIconBadgeNumber", [badge], null); this.call_native("stopPush", data, null);
}
} }
JPushPlugin.prototype.getApplicationIconBadgeNumber = function(callback) { JPushPlugin.prototype.resumePush = function() {
if(this.isPlatformIOS()) { data = [];
this.call_native("getApplicationIconBadgeNumber", [], callback); this.call_native("resumePush", data, null);
}
} }
JPushPlugin.prototype.isPushStopped = function(callback) {
data = [];
this.call_native("isPushStopped", data, callback);
}
// iOS methods
JPushPlugin.prototype.setTagsWithAlias = function(tags, alias) { JPushPlugin.prototype.setTagsWithAlias = function(tags, alias) {
try { try {
if(tags == null) { if(tags == null) {
@ -93,15 +97,6 @@ JPushPlugin.prototype.setAlias = function(alias) {
} }
} }
JPushPlugin.prototype.getRegistrationID = function(callback) {
try {
var data = [];
this.call_native("getRegistrationID", [data], callback);
} catch(exception) {
console.log(exception);
}
}
JPushPlugin.prototype.setBadge = function(value) { JPushPlugin.prototype.setBadge = function(value) {
if(this.isPlatformIOS()) { if(this.isPlatformIOS()) {
try { try {
@ -185,6 +180,57 @@ JPushPlugin.prototype.receiveMessageIniOSCallback = function(data) {
} }
} }
JPushPlugin.prototype.startLogPageView = function(pageName) {
if(this.isPlatformIOS()) {
this.call_native("startLogPageView", [pageName], null);
}
}
JPushPlugin.prototype.stopLogPageView = function(pageName) {
if(this.isPlatformIOS()) {
this.call_native("stopLogPageView", [pageName], null);
}
}
JPushPlugin.prototype.beginLogPageView = function(pageName, duration) {
if(this.isPlatformIOS()) {
this.call_native("beginLogPageView", [pageName, duration], null);
}
}
JPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge) {
if(this.isPlatformIOS()) {
this.call_native("setApplicationIconBadgeNumber", [badge], null);
}
}
JPushPlugin.prototype.getApplicationIconBadgeNumber = function(callback) {
if(this.isPlatformIOS()) {
this.call_native("getApplicationIconBadgeNumber", [], callback);
}
}
// Android methods
JPushPlugin.prototype.setDebugMode = function(mode) {
if(device.platform == "Android") {
this.call_native("setDebugMode", [mode], null);
}
}
JPushPlugin.prototype.setBasicPushNotificationBuilder = function() {
if(device.platform == "Android") {
data = [];
this.call_native("setBasicPushNotificationBuilder", data, null);
}
}
JPushPlugin.prototype.setCustomPushNotificationBuilder = function() {
if(device.platform == "Android") {
data = [];
this.call_native("setCustomPushNotificationBuilder", data, null);
}
}
JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data) { JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data) {
try { try {
console.log("JPushPlugin:receiveMessageInAndroidCallback"); console.log("JPushPlugin:receiveMessageInAndroidCallback");
@ -221,38 +267,6 @@ JPushPlugin.prototype.receiveNotificationInAndroidCallback = function(data) {
} }
} }
//android single
JPushPlugin.prototype.setBasicPushNotificationBuilder = function() {
if(device.platform == "Android") {
data = [];
this.call_native("setBasicPushNotificationBuilder", data, null);
}
}
JPushPlugin.prototype.setCustomPushNotificationBuilder = function() {
if(device.platform == "Android") {
data = [];
this.call_native("setCustomPushNotificationBuilder", data, null);
}
}
JPushPlugin.prototype.stopPush = function() {
data = [];
this.call_native("stopPush", data, null);
}
JPushPlugin.prototype.resumePush = function() {
data = [];
this.call_native("resumePush", data, null);
}
JPushPlugin.prototype.setDebugMode = function(mode) {
if(device.platform == "Android") {
this.call_native("setDebugMode", [mode], null);
}
}
JPushPlugin.prototype.clearAllNotification = function() { JPushPlugin.prototype.clearAllNotification = function() {
if(device.platform == "Android") { if(device.platform == "Android") {
data = []; data = [];
@ -273,21 +287,6 @@ JPushPlugin.prototype.setLatestNotificationNum = function(num) {
} }
} }
JPushPlugin.prototype.isPushStopped = function(callback) {
data = [];
this.call_native("isPushStopped", data, callback);
}
JPushPlugin.prototype.init = function() {
if(this.isPlatformIOS()) {
var data = [];
this.call_native("initial", data, null);
} else {
data = [];
this.call_native("init", data, null);
}
}
JPushPlugin.prototype.setDebugMode = function(mode) { JPushPlugin.prototype.setDebugMode = function(mode) {
if(device.platform == "Android") { if(device.platform == "Android") {
this.call_native("setDebugMode", [mode], null); this.call_native("setDebugMode", [mode], null);
@ -324,14 +323,26 @@ JPushPlugin.prototype.reportNotificationOpened = function(msgID) {
/** /**
*是否开启统计分析功能用于用户使用时长活跃用户用户打开次数的统计并上报到服务器上 *是否开启统计分析功能用于用户使用时长活跃用户用户打开次数的统计并上报到服务器上
* Portal 上展示给开发者 * Portal 上展示给开发者
**/ */
JPushPlugin.prototype.setStatisticsOpen = function(mode) { JPushPlugin.prototype.setStatisticsOpen = function(mode) {
if(device.platform == "Android") { if(device.platform == "Android") {
this.call_native("setStatisticsOpen", [mode], null); this.call_native("setStatisticsOpen", [mode], null);
} }
} }
//iOS single /**
* 用于在 Android 6.0 及以上系统申请一些权限
* 具体可看http://docs.jpush.io/client/android_api/#android-60
*/
JPushPlugin.prototype.requestPermission = function() {
if(device.platform == "Android") {
this.call_native("requestPermission", [], null);
}
}
JPushPlugin.prototype.setSilenceTime = function() {
}
if(!window.plugins) { if(!window.plugins) {
window.plugins = {}; window.plugins = {};