From 7fcefce3ce694c94c97b537c6e63ae91ba298ce9 Mon Sep 17 00:00:00 2001 From: zhangqinghe Date: Wed, 15 Apr 2015 16:59:32 +0800 Subject: [PATCH] fix android message and notification event --- README.md | 11 +++- document/Common_detail_api.md | 106 ++++++++++++++++++++++++++++++++++ document/iOS_detail_api.md | 65 +-------------------- example/index.html | 33 +++++------ src/android/JPushPlugin.java | 24 ++++++-- www/JPushPlugin.js | 17 +++++- 6 files changed, 166 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index c4db905..b76eda2 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,14 @@ window.plugins.jPushPlugin.setTagsWithAlias(tags,alias) window.plugins.jPushPlugin.setTags(tags) window.plugins.jPushPlugin.setAlias(alias) ++ 获取通知内容 + + event - jpush.openNotification + ++ 获取自定义消息推送内容 + + event - jpush.receiveMessage + [通用API详细说明](document/Common_detail_api.md) @@ -137,7 +145,6 @@ + 获取 APNs(通知) 推送内容 event - jpush.receiveNotification - event - jpush.openNotification + 获取自定义消息推送内容 @@ -175,6 +182,8 @@ + 接收推送消息Receiver + //推荐使用event jpush.Message和jpush.openNotification, + //下面这两个api 是兼容旧有的代码 window.plugins.jPushPlugin.receiveMessageInAndroidCallback(data) window.plugins.jPushPlugin.openNotificationInAndroidCallback(data) diff --git a/document/Common_detail_api.md b/document/Common_detail_api.md index fdff5b5..ffe6fa9 100644 --- a/document/Common_detail_api.md +++ b/document/Common_detail_api.md @@ -205,6 +205,8 @@ android iOS ####错误码定义 + + |Code|描述|详细解释| |-|-|-| |6001| 无效的设置,tag/alias 不应参数都为 null|| @@ -217,3 +219,107 @@ android iOS |6008| tag/alias 超出总长度限制。|总长度最多 1K 字节| |6011| 10s内设置tag或alias大于3次| 短时间内操作过于频繁| +### 获取通知内容 + +#### event - jpush.openNotification + +点击通知进入应用程序时会出发改事件 + +#####代码示例 + +- 在你需要接收通知的的js文件中加入: + + document.addEventListener("jpush.openNotification", onOpenNotification, false); + +- onOpenNotification需要这样写: + + + var alertContent + if(device.platform == "Android"){ + alertContent=window.plugins.jPushPlugin.openNotification.alert; + }else{ + alertContent = event.aps.alert; + } + alert("open Notificaiton:"+alertContent); + +ps:点击通知后传递的json object 保存在window.plugins.jPushPlugin.receiveNotification,直接访问即可,字段示例,根据实际推送情况,可能略有差别,请注意 + ++ android + + {"alert":"ding", + "extras":{ + "cn.jpush.android.MSG_ID":"1691785879", + "app":"com.thi.pushtest", + "cn.jpush.android.ALERT":"ding", + "cn.jpush.android.EXTRA":{}, + "cn.jpush.android.PUSH_ID":"1691785879", + "cn.jpush.android.NOTIFICATION_ID":1691785879, + "cn.jpush.android.NOTIFICATION_TYPE":"0"}} + ++ iOS + + { + "aps":{ + "badge":1, + "sound":"default", + "alert":"今天去哪儿" + }, + "_j_msgid":154604475 + } + + +### 获取自定义消息推送内容 + +### 获取自定义消息推送内容 + +####event - jpush.receiveMessage + +收到应用内消息时触发这个事件 + +`推荐使用事件的方式传递,但同时保留了receiveMessageIniOSCallback的回调函数,兼容以前的代码` + + +#####代码示例 + +- 在你需要接收通知的的js文件中加入: + + document.addEventListener("jpush.receiveMessage", onReceiveMessage, false); + +- onReceiveMessage需要这样写: + + + var onReceiveMessage = function(event){ + try{ + var message = window.plugins.jPushPlugin.receiveMessage.message; + //var extras = window.plugins.jPushPlugin.extras + + $("#messageResult").html(message); + + } + catch(exception){ + console.log("JPushPlugin:onReceiveMessage-->"+exception); + } + } + +ps:点击通知后传递的json object 保存在window.plugins.jPushPlugin.receiveNotification,直接访问即可,字段示例,根据实际推送情况,可能略有差别,请注意 + ++ android + + {"message":"今天去哪儿", + "extras"{ + "cn.jpush.android.MSG_ID":"154378013", + "cn.jpush.android.CONTENT_TYPE":"", + "cn.jpush.android.EXTRA":{"key":"不添没有"} + } + } ++ iOS + + { + "content":"今天去哪儿", + "extras": + { + "key":"不填写没有" + } + } + + diff --git a/document/iOS_detail_api.md b/document/iOS_detail_api.md index 4b0a970..4ad8e11 100644 --- a/document/iOS_detail_api.md +++ b/document/iOS_detail_api.md @@ -24,70 +24,7 @@ console.log(exception) } } - -#### event - jpush.openNotification - -点击通知进入应用程序时会出发改事件 - -#####代码示例 - -- 在你需要接收通知的的js文件中加入: - - document.addEventListener("jpush.openNotification", onOpenNotification, false); - -- onOpenNotification需要这样写: - - - var onOpenNotification = function(event){ - try{ - - var alert = event.aps.alert; - console.log("JPushPlugin:onPpenNotification key aps.alert:"+alert); - } - catch(exception){ - console.log("JPushPlugin:onPpenNotification"+exception); - } - } - - - - -### 获取自定义消息推送内容 - -####event - jpush.receiveMessage - -收到应用内消息时触发这个事件 - -`推荐使用事件的方式传递,但同时保留了receiveMessageIniOSCallback的回调函数,兼容以前的代码` - - -#####代码示例 - -- 在你需要接收通知的的js文件中加入: - - document.addEventListener("jpush.receiveMessage", onReceiveMessage, false); - -- onReceiveMessage需要这样写: - - - var onReceiveMessage = function(event){ - try{ - var eventMessage="{"; - for(var key in event){ - if(key=="type"){ - break - } - eventMessage+=key+":"+JSON.stringify(event[key])+"\n" - console.log(key+":"+JSON.stringify(event[key])); - - } - eventMessage+="}"; - $("#messageResult").html(eventMessage); - } - catch(exception){ - console.log("JPushPlugin:onReceiveMessage"+exception); - } - } + diff --git a/example/index.html b/example/index.html index 9f0d715..1e472fd 100644 --- a/example/index.html +++ b/example/index.html @@ -39,12 +39,17 @@ } var onOpenNotification = function(event){ try{ - - var alertContent = event.aps.alert; + var alertContent + if(device.platform == "Android"){ + alertContent=window.plugins.jPushPlugin.openNotification.alert; + }else{ + alertContent = event.aps.alert; + } alert("open Notificaiton:"+alertContent); + } catch(exception){ - console.log("JPushPlugin:onPpenNotification"+exception); + console.log("JPushPlugin:onOpenNotification"+exception); } } var onReceiveNotification = function(event){ @@ -67,22 +72,14 @@ } var onReceiveMessage = function(event){ try{ - - var eventMessage="{"; - for(var key in event){ - if(key=="type"){ - break - } - eventMessage+=key+":"+JSON.stringify(event[key])+"\n" - console.log(key+":"+JSON.stringify(event[key])); - - } - eventMessage+="}"; - $("#messageResult").html(eventMessage); - + var message = window.plugins.jPushPlugin.receiveMessage.message; + //var extras = window.plugins.jPushPlugin.extras + + $("#messageResult").html(message); + } catch(exception){ - console.log("JPushPlugin:onReceiveMessage"+exception); + console.log("JPushPlugin:onReceiveMessage-->"+exception); } } @@ -92,7 +89,7 @@ window.plugins.jPushPlugin.init(); window.plugins.jPushPlugin.getRegistrationID(onGetRegistradionID); - if(JPushPlugin.prototype.isPlatformIOS()){ + if(device.platform == "Android"){ window.plugins.jPushPlugin.setDebugModeFromIos(); window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0); }else{ diff --git a/src/android/JPushPlugin.java b/src/android/JPushPlugin.java index 30c6c14..8673a58 100644 --- a/src/android/JPushPlugin.java +++ b/src/android/JPushPlugin.java @@ -126,8 +126,15 @@ public class JPushPlugin extends CordovaPlugin { String js = String .format("window.plugins.jPushPlugin.receiveMessageInAndroidCallback('%s');", data.toString()); + + try { instance.webView.sendJavascript(js); + +// String jsEvent=String +// .format("cordova.fireDocumentEvent('jpush.receiveMessage',%s)", +// data.toString()); +// instance.webView.sendJavascript(jsEvent); } catch (NullPointerException e) { } catch (Exception e) { @@ -142,13 +149,22 @@ public class JPushPlugin extends CordovaPlugin { String js = String .format("window.plugins.jPushPlugin.openNotificationInAndroidCallback('%s');", data.toString()); +// {"alert":"ding", +// "extras":{ +// "cn.jpush.android.MSG_ID":"1691785879", +// "app":"com.thi.pushtest", +// "cn.jpush.android.ALERT":"ding", +// "cn.jpush.android.EXTRA":{}, +// "cn.jpush.android.PUSH_ID":"1691785879", +// "cn.jpush.android.NOTIFICATION_ID":1691785879, +// "cn.jpush.android.NOTIFICATION_TYPE":"0"}} try { instance.webView.sendJavascript(js); - String jsEvent=String - .format("cordova.fireDocumentEvent('jpush.openNotification',%s)", - data.toString()); - instance.webView.sendJavascript(jsEvent); +// String jsEvent=String +// .format("cordova.fireDocumentEvent('jpush.openNotification',%s)", +// data.toString()); +// instance.webView.sendJavascript(jsEvent); } catch (NullPointerException e) { } catch (Exception e) { diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index a8753d9..8867bab 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -1,8 +1,13 @@ var JPushPlugin = function(){ }; + //private plugin function +JPushPlugin.prototype.receiveMessage={} +JPushPlugin.prototype.openNotification={} + + JPushPlugin.prototype.isPlatformIOS = function(){ return device.platform == "iPhone" || device.platform == "iPad" || device.platform == "iPod touch" || device.platform == "iOS" } @@ -140,8 +145,10 @@ JPushPlugin.prototype.receiveMessageIniOSCallback = function(data){ JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){ try{ console.log("JPushPlugin:receiveMessageInAndroidCallback"); + var bToObj = JSON.parse(data); + this.openNotification=bToObj + cordova.fireDocumentEvent('jpush.receiveMessage',null); //console.log(data); - //ecvar bToObj=JSON.parse(data); //var message = bToObj.message; //var extras = bToObj.extras; @@ -158,8 +165,12 @@ JPushPlugin.prototype.receiveMessageInAndroidCallback = function(data){ // JPushPlugin.prototype.openNotificationInAndroidCallback = function(data){ try{ - console.log("JPushPlugin:openNotificationInAndroidCallback"); - console.log(data); + console.log("JPushPlugin:openNotificationInAndroidCallback"); + var bToObj = JSON.parse(data); + this.receiveNotification=bToObj; + cordova.fireDocumentEvent('jpush.openNotification',null); + + //console.log(data); //var bToObj = JSON.parse(data); //var alert = bToObj.alert; //var extras = bToObj.extras;