解决当应用在后台时无法获取通知标题的 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 {
private final static List<String> methodList =
Arrays.asList(
"getRegistrationID",
"setTags",
"setTagsWithAlias",
"setAlias",
"getNotification",
"setBasicPushNotificationBuilder",
"setCustomPushNotificationBuilder",
"setPushTime",
"init",
"setDebugMode",
"stopPush",
"resumePush",
"isPushStopped",
"setLatestNotificationNum",
"setPushTime",
"clearAllNotification",
"clearNotificationById",
"addLocalNotification",
"removeLocalNotification",
"clearLocalNotifications",
"onResume",
"onPause",
"reportNotificationOpened",
"setStatisticsOpen");
"addLocalNotification",
"clearAllNotification",
"clearLocalNotifications",
"clearNotificationById",
"getNotification",
"getRegistrationID",
"init",
"isPushStopped",
"onPause",
"onResume",
"requestPermission",
"removeLocalNotification",
"reportNotificationOpened",
"resumePush",
"setAlias",
"setBasicPushNotificationBuilder",
"setCustomPushNotificationBuilder",
"setDebugMode",
"setLatestNotificationNum",
"setPushTime",
"setTags",
"setTagsWithAlias",
"setSilenceTime",
"setStatisticsOpen",
"stopPush"
);
private ExecutorService threadPool = Executors.newFixedThreadPool(1);
private static JPushPlugin instance;
@ -95,7 +97,7 @@ public class JPushPlugin extends CordovaPlugin {
//这样做是为了和 iOS 统一
if (openNotificationAlert != null) {
notificationAlert = null;
transmitNotificationOpen(notificationTitle, openNotificationAlert,
transmitNotificationOpen(openNotificationTitle, openNotificationAlert,
openNotificationExtras);
}
if (notificationAlert != null) {
@ -121,7 +123,7 @@ public class JPushPlugin extends CordovaPlugin {
}
if (openNotificationAlert != null) {
notificationAlert = null;
transmitNotificationOpen(notificationTitle, openNotificationAlert,
transmitNotificationOpen(openNotificationTitle, openNotificationAlert,
openNotificationExtras);
}
if (notificationAlert != null) {
@ -209,9 +211,6 @@ public class JPushPlugin extends CordovaPlugin {
if (instance == null) {
return;
}
if (JPushPlugin.shouldCacheMsg) {
return;
}
JSONObject data = getNotificationObject(title, alert, extras);
String format = "window.plugins.jPushPlugin.openNotificationInAndroidCallback(%s);";
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() {
@Override
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;
}
//public plugin function
JPushPlugin.prototype.startLogPageView = function(pageName) {
// public methods
JPushPlugin.prototype.init = function() {
if(this.isPlatformIOS()) {
this.call_native("startLogPageView", [pageName], null);
}
}
JPushPlugin.prototype.stopLogPageView = function(pageName) {
if(this.isPlatformIOS()) {
this.call_native("stopLogPageView", [pageName], null);
var data = [];
this.call_native("initial", data, null);
} else {
data = [];
this.call_native("init", data, null);
}
}
JPushPlugin.prototype.beginLogPageView = function(pageName, duration) {
if(this.isPlatformIOS()) {
this.call_native("beginLogPageView", [pageName, duration], null);
JPushPlugin.prototype.getRegistrationID = function(callback) {
try {
var data = [];
this.call_native("getRegistrationID", [data], callback);
} catch(exception) {
console.log(exception);
}
}
JPushPlugin.prototype.setApplicationIconBadgeNumber = function(badge) {
if(this.isPlatformIOS()) {
this.call_native("setApplicationIconBadgeNumber", [badge], null);
}
JPushPlugin.prototype.stopPush = function() {
data = [];
this.call_native("stopPush", data, null);
}
JPushPlugin.prototype.getApplicationIconBadgeNumber = function(callback) {
if(this.isPlatformIOS()) {
this.call_native("getApplicationIconBadgeNumber", [], callback);
}
JPushPlugin.prototype.resumePush = function() {
data = [];
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) {
try {
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) {
if(this.isPlatformIOS()) {
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) {
try {
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() {
if(device.platform == "Android") {
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) {
if(device.platform == "Android") {
this.call_native("setDebugMode", [mode], null);
@ -324,14 +323,26 @@ JPushPlugin.prototype.reportNotificationOpened = function(msgID) {
/**
*是否开启统计分析功能用于用户使用时长活跃用户用户打开次数的统计并上报到服务器上
* Portal 上展示给开发者
**/
*/
JPushPlugin.prototype.setStatisticsOpen = function(mode) {
if(device.platform == "Android") {
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) {
window.plugins = {};