Skip to content

Commit

Permalink
implement event
Browse files Browse the repository at this point in the history
  • Loading branch information
Gen Liu authored and Gen Liu committed Aug 6, 2015
1 parent 8763f60 commit 9a66efe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/ios/CDVXGPushPlugin.h
Expand Up @@ -8,6 +8,10 @@
- (void) didFailToRegisterForRemoteNotificationsWithError:(NSNotification*)notification;
- (void) didReceiveRemoteNotification:(NSNotification*)notification;

- (void) registerpush:(CDVInvokedUrlCommand*)command;
- (void) unregisterpush:(CDVInvokedUrlCommand*)command;
- (void) addlistener:(CDVInvokedUrlCommand*)command;

@property CDVXGPushUtil* util;
@property NSMutableArray* callbackIds;

Expand Down
34 changes: 27 additions & 7 deletions src/ios/CDVXGPushPlugin.m
Expand Up @@ -18,6 +18,7 @@ - (void) pluginInitialize {

// 初始化
self.util = [[CDVXGPushUtil alloc] init];
self.callbackIds = [[NSMutableArray alloc] init];

// 启动 XGPush
[self.util startApp];
Expand Down Expand Up @@ -53,10 +54,12 @@ - (void) didFailToRegisterForRemoteNotificationsWithError:(NSNotification*)notif
*/
- (void) didReceiveRemoteNotification:(NSNotification*)notification {
NSLog(@"[XGPushPlugin] receive notification: %@", notification);
NSLog(@"[XGPushPlugin] callback ids: %@", self.callbackIds);
[self.callbackIds enumerateObjectsUsingBlock:^(id callbackId, NSUInteger idx, BOOL *stop) {
NSLog(@"[XGPushPlugin] callbackId: %@", callbackId);
// CDVPluginResult* result = nil;
// [self.commandDelegate sendPluginResult:result callbackId:callbackId];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:notification.object];
[result setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}];
}

Expand All @@ -68,20 +71,20 @@ - (void) didReceiveRemoteNotification:(NSNotification*)notification {
*/
- (void) registerpush:(CDVInvokedUrlCommand*)command {
NSString* alias = [command.arguments objectAtIndex:0];
NSLog(@"[XGPushPlugin] registerpush");
__block CDVXGPushPlugin* blocksafeSelf = self;
NSLog(@"[XGPushPlugin] registerpush: %@", alias);

// FIXME: 放到 background thread 里运行时无法执行回调
[self.util registerPush:alias successCallback:^{
// 成功
NSLog(@"[XGPushPlugin] registerpush success");
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[blocksafeSelf.commandDelegate sendPluginResult:result callbackId:command.callbackId];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];

} errorCallback:^{
// 失败
NSLog(@"[XGPushPlugin] registerpush error");
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[blocksafeSelf.commandDelegate sendPluginResult:result callbackId:command.callbackId];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}];
}

Expand All @@ -91,7 +94,23 @@ - (void) registerpush:(CDVInvokedUrlCommand*)command {
* @param {[type]} void [description]
* @return {[type]} [description]
*/
- (void) unregisterpush:(CDVXGPushPlugin*)command {
- (void) unregisterpush:(CDVInvokedUrlCommand*)command {
NSLog(@"[XGPushPlugin] registerpush");

// FIXME: 放到 background thread 里运行时无法执行回调
[self.util unregisterPush:^{
// 成功
NSLog(@"[XGPushPlugin] deregisterpush success");
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];

} errorCallback:^{
// 失败
NSLog(@"[XGPushPlugin] deregisterpush error");
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];

}];
}

/**
Expand All @@ -101,6 +120,7 @@ - (void) unregisterpush:(CDVXGPushPlugin*)command {
* @return {[type]} [description]
*/
- (void) addlistener:(CDVInvokedUrlCommand*)command {
NSLog(@"[XGPushPlugin] add listener: %@", command.callbackId);
[self.callbackIds addObject:command.callbackId];
}

Expand Down
5 changes: 4 additions & 1 deletion src/ios/CDVXGPushUtil.h
@@ -1,11 +1,14 @@
#import <Foundation/Foundation.h>

typedef void (^CallbackBlock)(void);

@interface CDVXGPushUtil: NSObject

- (void) startApp;
- (void) initForReregister;
- (void) registerDevice:(NSData*)deviceToken;
- (void) registerPush:(NSString*)alias successCallback:(void (^)(void))success errorCallback:(void (^)(void))error;
- (void) registerPush:(NSString*)alias successCallback:(CallbackBlock)success errorCallback:(CallbackBlock)error;
- (void) unregisterPush:(CallbackBlock)success errorCallback:(CallbackBlock)error;
- (uint32_t) getAccessID;
- (NSString*) getAccessKey;

Expand Down
13 changes: 9 additions & 4 deletions src/ios/CDVXGPushUtil.m
Expand Up @@ -15,7 +15,6 @@ - (void) initForReregister {
[CDVRegisterNotification registerNotification];
}
}];
return self;
}

/**
Expand All @@ -26,7 +25,6 @@ - (void) startApp {
NSString* accessKey = [self getAccessKey];
NSLog(@"[XGPush] starting with access id: %u, access key: %@", accessID, accessKey);
[XGPush startApp:accessID appKey:accessKey];
return self;
}

/**
Expand All @@ -38,7 +36,6 @@ - (void) startApp {
- (void) registerDevice:(NSData*)deviceToken {
NSLog(@"[XGPush] register with token:%@", deviceToken);
self.deviceToken = deviceToken;
return self;
}

/**
Expand All @@ -47,15 +44,23 @@ - (void) registerDevice:(NSData*)deviceToken {
* @param {[type]} void [description]
* @return {[type]} [description]
*/
- (void) registerPush:(NSString*)alias successCallback:(void (^)(void))success errorCallback:(void (^)(void))error {
- (void) registerPush:(NSString*)alias successCallback:(CallbackBlock)success errorCallback:(CallbackBlock)error {
NSLog(@"[XGPush] register with token:%@ alias:%@", self.deviceToken, alias);
if ([alias respondsToSelector:@selector(length)] && [alias length] > 0) {
NSLog(@"[XGPush] setting alias:%@", alias);
[XGPush setAccount:alias];
}

[XGPush registerDevice:self.deviceToken successCallback:success errorCallback:error];
}

/**
* 停止接收
*/
- (void)unregisterPush:(CallbackBlock)success errorCallback:(CallbackBlock)error {
[XGPush unRegisterDevice:success errorCallback:error];
}

/**
* 获取 access id
*
Expand Down

0 comments on commit 9a66efe

Please sign in to comment.