add notificatoncenter and track page

This commit is contained in:
zhangqinhghe 2014-06-05 16:36:54 +08:00
parent 334cda7486
commit 599952976f
5 changed files with 245 additions and 16 deletions

View File

@ -14,28 +14,98 @@
<script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var onDeviceReady = function() {
var onDeviceReady = function() {
console.log("Device ready!") console.log("Device ready!")
initiateUI(); initiateUI();
}
var onTagsWithAlias = function(data) {
try{
var tagaliasJson=window.plugins.jPushPlugin.parseEvent(data);
$("#tagAliasResult").html(tagaliasJson);;
} }
var initiateUI = function() { catch(exception){
$("#setTagWithAliasButton").click(function(ev) { console.log(exception)
console.log("tap set tag/alias button!"); }
}
var onDidSetup = function() {
try{
$("#connectState").html("建立连接");;
}
catch(exception){
console.log(exception)
}
}
var onDidClose = function() {
try{
$("#connectState").html("连接关闭");;
}
catch(exception){
console.log(exception)
}
}
var onDidRegister = function() {
try{
$("#connectState").html("注册成功");;
}
catch(exception){
console.log(exception)
}
}
var onDidLogin = function() {
try{
$("#connectState").html("登录成功");;
}
catch(exception){
console.log(exception)
}
}
var onGetRegistradionID = function(data) {
try{
var registrationID=window.plugins.jPushPlugin.parseEvent(data);
$("#registrationid").html(registrationID);;
}
catch(exception){
console.log(exception)
}
}
var initiateUI = function() {
window.plugins.jPushPlugin.getRegistrationID();
window.plugins.jPushPlugin.initNotificationCenter();
window.plugins.jPushPlugin.startLogPageView("mianPage");
//window.plugins.jPushPlugin.stopLogPageView("mianPage");
$("#setTagWithAliasButton").click(function(ev) {
try{ try{
var tag1 = $("#tagText1").attr("value"); var tag1 = $("#tagText1").attr("value");
console.log("tag1:"+tag1); console.log("tag1:"+tag1);
window.plugins.jPushPlugin.setTagsWithAlias("myTags","myTags1","myTags2","myAlias"); window.plugins.jPushPlugin.setTagsWithAlias("myTags","myTags1","myTags2","myAlias");
} }
catch(exception){ catch(exception){
console.log(exception); console.log(exception);
} }
}) })
} }
document.addEventListener("networkDidSetup", onDidSetup, false)
document.addEventListener("networkDidClose", onDidClose, false)
document.addEventListener("networkDidRegister", onDidRegister, false)
document.addEventListener("networkDidLogin", onDidLogin, false)
document.addEventListener("setTagsWithAlias", onTagsWithAlias, false)
document.addEventListener("registrationID", onGetRegistradionID, false)
document.addEventListener("deviceready", onDeviceReady, false) document.addEventListener("deviceready", onDeviceReady, false)
</script> </script>
</head> </head>
<body> <body>
@ -53,6 +123,12 @@
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<center><h3>JPushPlugin Example</h3></center> <center><h3>JPushPlugin Example</h3></center>
<span name="alias" id="alias"></span><hr /> <span name="alias" id="alias"></span><hr />
<label>RegistrationID: </label>
<label id="registrationid">null</label>
</div>
<div>
<label>connected state: </label>
<label id="connectState">未连接</label>
</div> </div>
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<label>Tags: </label> <label>Tags: </label>
@ -85,6 +161,10 @@
<div data-role="fieldcontain"> <div data-role="fieldcontain">
<input type="button" id="setTagWithAliasButton" value="Add tag and alias" /> <input type="button" id="setTagWithAliasButton" value="Add tag and alias" />
</div> </div>
<div data-role="fieldcontain">
<label id="tagAliasResult" >null</label>
</div>
</div> </div>
</form> </form>
</div> </div>

View File

@ -41,8 +41,9 @@
<framework src="CoreFoundation.framework" weak="true" /> <framework src="CoreFoundation.framework" weak="true" />
<framework src="UIKit.framework" weak="true" /> <framework src="UIKit.framework" weak="true" />
<framework src="CoreTelephony.framework" weak="true" /> <framework src="CoreTelephony.framework" weak="true" />
<framework src="Security.framework" weak="true" />
<framework src="libz.dylib" weak="true" />
</platform> </platform>
<!-- android --> <!-- android -->

View File

@ -16,4 +16,8 @@
-(void)setTags:(CDVInvokedUrlCommand*)command; -(void)setTags:(CDVInvokedUrlCommand*)command;
-(void)setAlias:(CDVInvokedUrlCommand*)command; -(void)setAlias:(CDVInvokedUrlCommand*)command;
-(void)getRegistrationID:(CDVInvokedUrlCommand*)command; -(void)getRegistrationID:(CDVInvokedUrlCommand*)command;
-(void)initNotifacationCenter:(CDVInvokedUrlCommand*)command;
-(void)startLogPageView:(CDVInvokedUrlCommand*)command;
-(void)stopLogPageView:(CDVInvokedUrlCommand*)command;
@end @end

View File

@ -10,7 +10,8 @@
#import "APService.h" #import "APService.h"
@implementation JPushPlugin @implementation JPushPlugin
-(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{ -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command{
NSArray *arguments=command.arguments; NSArray *arguments=command.arguments;
@ -55,11 +56,23 @@
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} }
-(void)getRegistrationID:(CDVInvokedUrlCommand*)command{
NSString* registratonID = [APService registrionID];
dispatch_async(dispatch_get_main_queue(), ^{
//
NSString *script=[NSString stringWithFormat:@"cordova.fireDocumentEvent('registrationID','[%@]')",registratonID];
[self.commandDelegate evalJs:script];
[self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.registrationCallback('%@')",registratonID]];
});
}
-(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{ -(void)tagsWithAliasCallback:(int)resultCode tags:(NSSet *)tags alias:(NSString *)alias{
//NSLog(@"recode is %d tags is %@ alias %@",resultCode,tags,alias); NSLog(@"recode is %d tags is %@ alias %@",resultCode,tags,alias);
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys: NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:resultCode],@"resultCode", [NSNumber numberWithInt:resultCode],@"resultCode",
tags==nil?[NSNull null]:[tags allObjects],@"resultTags", tags==nil?[NSNull null]:[tags allObjects],@"resultTags",
@ -68,12 +81,71 @@
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('setTagsWithAlias','[%@]')",jsonString]];
[self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.pushCallback('%@')",jsonString]]; [self writeJavascript:[NSString stringWithFormat:@"window.plugins.jPushPlugin.pushCallback('%@')",jsonString]];
}); });
} }
-(void)getRegistrationID:(CDVInvokedUrlCommand*)command{ - (void)networkDidSetup:(NSNotification *)notification {
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidSetup')"]];
}
- (void)networkDidClose:(NSNotification *)notification {
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidClose')"]];
}
- (void)networkDidRegister:(NSNotification *)notification {
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidRegister')"]];
}
- (void)networkDidLogin:(NSNotification *)notification {
[self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('networkDidLogin')"]];
}
-(void)initNotifacationCenter:(CDVInvokedUrlCommand*)command{
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(networkDidSetup:)
name:kAPNetworkDidSetupNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidClose:)
name:kAPNetworkDidCloseNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidRegister:)
name:kAPNetworkDidRegisterNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidLogin:)
name:kAPNetworkDidLoginNotification
object:nil];
}
-(void)startLogPageView:(CDVInvokedUrlCommand*)command{
NSArray *arguments=command.arguments;
if (!arguments||[arguments count]<1) {
NSLog(@"startLogPageView argument error");
return ;
}
NSString * pageName=[arguments objectAtIndex:0];
if (pageName) {
[APService startLogPageView:pageName];
}
}
-(void)stopLogPageView:(CDVInvokedUrlCommand*)command{
NSArray *arguments=command.arguments;
if (!arguments||[arguments count]<1) {
NSLog(@"stopLogPageView argument error");
return ;
}
NSString * pageName=[arguments objectAtIndex:0];
if (pageName) {
[APService stopLogPageView:pageName];
}
} }

View File

@ -1,4 +1,4 @@
cordova.define("cn.jpush.phonegap.JPushPlugin.JPushPlugin", function(require, exports, module) {
var JPushPlugin = function(){ var JPushPlugin = function(){
}; };
@ -11,6 +11,67 @@ JPushPlugin.prototype.call_native = function ( name, args) {
args); args);
return ret; return ret;
} }
JPushPlugin.prototype.getRegistrationID = function () {
this.call_native( "getRegistrationID", null);
}
JPushPlugin.prototype.startLogPageView = function (data) {
if (data==null || typeof(data)=="undefined" || data==""){
console.log("argument is null");
}
else{
this.call_native( "startLogPageView", [data]);
}
}
JPushPlugin.prototype.stopLogPageView = function (data) {
if (data==null || typeof(data)=="undefined" || data==""){
console.log("argument is null");
}
else{
this.call_native( "stopLogPageView", [data]);
}
}
JPushPlugin.prototype.initNotificationCenter = function () {
this.call_native( "initNotifacationCenter", null);
}
JPushPlugin.prototype.parseEvent = function (data) {
try{
var parament=""
var count=1;
var start=false;
for(var i in data){
if(data[i]=='['){
if (count==1&&start==false) {
start=true;
continue;
}
count++;
}
if (data[i]==']') {
if(count==1)
{
break;
}
count--;
}
parament+=data[i]
}
return parament;
}
catch(exception){
alert(exception);
}
}
JPushPlugin.prototype.setTagsWithAlias = function (tags,alias) { JPushPlugin.prototype.setTagsWithAlias = function (tags,alias) {
@ -52,12 +113,21 @@ JPushPlugin.prototype.pushCallback = function (data) {
var code = bToObj.resultCode; var code = bToObj.resultCode;
var tags = bToObj.resultTags; var tags = bToObj.resultTags;
var alias = bToObj.resultAlias; var alias = bToObj.resultAlias;
console.log("JPushPlugin:callBack--code is "+code+" tags is "+tags + " alias is "+alias);
} }
catch(exception){ catch(exception){
alert(exception); alert(exception);
} }
} }
JPushPlugin.prototype.registrationCallback = function (data) {
try{
console.log("registrationCallback--registraionID is "+data);
}
catch(exception){
alert(exception);
}
}
if(!window.plugins) { if(!window.plugins) {
window.plugins = {}; window.plugins = {};
} }
@ -65,3 +135,5 @@ if(!window.plugins.jPushPlugin){
window.plugins.jPushPlugin = new JPushPlugin(); window.plugins.jPushPlugin = new JPushPlugin();
} }
module.exports = new JPushPlugin(); module.exports = new JPushPlugin();
});