diff --git a/.gitignore b/.gitignore index e26a231..1533896 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -.project -.tmp/ +.* node_modules/ diff --git a/README.md b/README.md index d0de132..3e54cee 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ 腾讯信鸽推送服务:http://xg.qq.com/ -信鸽 SDK 版本:2.40 +信鸽 Android SDK 版本:2.40 + +信鸽 iOS SDK 版本:2.4.0 Cordova 版本:3.x / 4.x / 5.x diff --git a/package.json b/package.json index 2c7482e..f6f29ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xgpush-cordova", - "version": "0.1.3", + "version": "0.2.0", "description": "腾讯信鸽推送服务:http://xg.qq.com/", "main": "index.js", "dependencies": { diff --git a/plugin.xml b/plugin.xml index a957f48..9d0b4e4 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + id="cordova-plugin-xgpush" + version="0.2.0"> xgpush-cordova Tencent XGPush Plugin @@ -147,6 +147,53 @@ + + + + + + + + + + + + + AccessID + $ACCESS_ID + AccessKey + $ACCESS_KEY + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/android/v2/XGCordova.java b/src/android/v2/XGCordova.java index 5fc5968..50eb200 100644 --- a/src/android/v2/XGCordova.java +++ b/src/android/v2/XGCordova.java @@ -10,7 +10,7 @@ import android.util.Log; public class XGCordova extends XGPushPlugin { - + private static final String TAG = "XGCordovaPlugin"; private ReceiverManager manager = new ReceiverManager(); @@ -38,7 +38,7 @@ protected boolean unregisterPush(Context context, CallbackContext callback) { @Override protected boolean addListener(Context context, CallbackContext callback) { - return manager.registerReceiver(context, callback); + return manager.registerReceiver(context, callback); } } diff --git a/src/ios/2.4.0/XGPush.h b/src/ios/2.4.0/XGPush.h new file mode 100755 index 0000000..24c7c15 --- /dev/null +++ b/src/ios/2.4.0/XGPush.h @@ -0,0 +1,178 @@ +// +// XGPush.h +// XG-SDK +// +// Created by xiangchen on 13-10-18. +// Copyright (c) 2013年 mta. All rights reserved. +// + +#import +#import + +#define XG_SDK_VERSION @"2.4.0" + +@interface XGPush : NSObject + +/** + * 初始化信鸽 + * @param appId - 通过前台申请的应用ID + * @param appKey - 通过前台申请的appKey + * @return none + */ ++(void)startApp:(uint32_t)appId appKey:(NSString *)appKey; + +/** + * 如果注销过信鸽,需要再次注册push服务前的准备 + * @param successCallback 初始化之后的回调,如果需要即刻恢复push服务,registerPush在此回调中调用 + * @return none + */ ++(void)initForReregister:(void (^)(void)) successCallback; + +/** + * 判断当前是否是已注销状态 + * @param none + * @return none + */ ++(BOOL)isUnRegisterStatus; + +/** + * 设置设备的帐号 (在初始化信鸽后,注册设备之前调用。account本质上是registerDevice的一个参数) + * @param account - 帐号名(长度为2个字节以上,不要使用"test","123456"这种过于简单的字符串) + * @return none + */ ++(void)setAccount:(NSString *)account; + +/** + * 注册设备 + * @param deviceToken - 通过app delegate的didRegisterForRemoteNotificationsWithDeviceToken回调的获取 + * @return 获取的deviceToken字符串 + */ ++(NSString *)registerDevice:(NSData *)deviceToken; + +//注册设备,支持回调函数版本 ++(NSString *)registerDevice:(NSData *)deviceToken successCallback:(void (^)(void)) successCallback errorCallback:(void (^)(void)) errorCallback; + +//注册设备,支持字符串deviceToken版本 ++(NSString *)registerDeviceStr:(NSString *)deviceToken; + +/** + * 注销设备,设备不再进行推送 + * @param none + * @return none + */ ++(void)unRegisterDevice; + +//注销设备,支持回调版本 ++(void)unRegisterDevice:(void (^)(void)) successCallback errorCallback:(void (^)(void)) errorCallback; + +/** + * 设置tag + * @param tag - 需要设置的tag + * @return none + */ ++(void)setTag:(NSString *)tag; + +//设置tag,支持回调版本 ++(void)setTag:(NSString *)tag successCallback:(void (^)(void)) successCallback errorCallback:(void (^)(void)) errorCallback; + +/** + * 删除tag + * @param tag - 需要删除的tag + * @return none + */ ++(void)delTag:(NSString *)tag; + +//删除tag,支持回调版本 ++(void)delTag:(NSString *)tag successCallback:(void (^)(void)) successCallback errorCallback:(void (^)(void)) errorCallback; + +/** + * 在didReceiveRemoteNotification中调用,用于推送反馈。(app在运行时) + * @param userInfo 苹果apns的推送信息 + * @return none + */ ++(void)handleReceiveNotification:(NSDictionary *)userInfo; + +//信鸽Pro专用接口 ++(void)handleReceiveNotification:(NSDictionary *)userInfo completion:(void (^)(void)) completion; + +//推送反馈(app在运行时),支持回调版本 ++(void)handleReceiveNotification:(NSDictionary *)userInfo successCallback:(void (^)(void)) successCallback errorCallback:(void (^)(void)) errorCallback completion:(void (^)(void)) completion; + +/** + * 在didFinishLaunchingWithOptions中调用,用于推送反馈.(app没有运行时,点击推送启动时) + * @param userInfo + * @return none + */ ++(void)handleLaunching:(NSDictionary *)launchOptions; + +//推送反馈.(app没有运行时,点击推送启动时),支持回调版本 ++(void)handleLaunching:(NSDictionary *)launchOptions successCallback:(void (^)(void)) successCallback errorCallback:(void (^)(void)) errorCallback; + +/** + * 获取userInfo里的bid信息(bid:信鸽的推送消息id) + * @param userInfo 苹果apns的推送信息 + * @return none + */ ++(NSString *)getBid:(NSDictionary *)userInfo; + +/** + * deviceToken类型转换 + * @param deviceToken NSData格式的deviceToken + * @return none + */ ++(NSString *)getDeviceToken:(NSData *)deviceToken; + +/** + * 获取本地缓存的deviceToken + * @param none + * @return 信鸽启动时填入的的accessID + */ ++(uint32_t)getAccessID; + +/************************** + 以下是本地推送相关 +**************************/ + +/** + * 本地推送,最多支持64个 + * @param fireDate 本地推送触发的时间 + * @param alertBody 推送的内容 + * @param badge 角标的数字。如果不改变,则传递 -1 + * @param alertAction 替换弹框的按钮文字内容(默认为"启动") + * @param userInfo 自定义参数,可以用来标识推送和增加附加信息 + * @return none + */ ++(void)localNotification:(NSDate *)fireDate alertBody:(NSString *)alertBody badge:(int)badge alertAction:(NSString *)alertAction userInfo:(NSDictionary *)userInfo; + +/** + * 本地推送在前台推送。默认App在前台运行时不会进行弹窗,通过此接口可实现指定的推送弹窗。 + * @param notification 本地推送对象 + * @param userInfoKey 本地推送的标识Key + * @param userInfoValue 本地推送的标识Key对应的值 + * @return none + */ ++(void)localNotificationAtFrontEnd:(UILocalNotification *)notification userInfoKey:(NSString *)userInfoKey userInfoValue:(NSString *)userInfoValue; + +/** + * 删除本地推送,方法1 + * @param userInfoKey 本地推送的标识Key + * @param userInfoValue 本地推送的标识Key对应的值 + * @return none + */ ++(void)delLocalNotification:(NSString *)userInfoKey userInfoValue:(NSString *)userInfoValue; + +/** + * 删除本地推送,方法2 + * @param myUILocalNotification 本地推送对象 + * @return none + */ ++(void)delLocalNotification:(UILocalNotification *)myUILocalNotification; + +/** + * 清除所有本地推送对象 + * @param none + * @return none + */ ++(void)clearLocalNotifications; + +@end \ No newline at end of file diff --git a/src/ios/2.4.0/XGSetting.h b/src/ios/2.4.0/XGSetting.h new file mode 100755 index 0000000..a7bcd8a --- /dev/null +++ b/src/ios/2.4.0/XGSetting.h @@ -0,0 +1,17 @@ +// +// XGSetting.h +// XG-SDK +// +// Created by xiangchen on 29/08/14. +// Copyright (c) 2014 mta. All rights reserved. +// + +#import + +@interface XGSetting : NSObject + +@property (nonatomic,retain) NSString* Channel; +@property (nonatomic,retain) NSString* GameServer; + ++(id)getInstance; +@end diff --git a/src/ios/2.4.0/libXG-SDK.a b/src/ios/2.4.0/libXG-SDK.a new file mode 100755 index 0000000..bd7e5e5 Binary files /dev/null and b/src/ios/2.4.0/libXG-SDK.a differ diff --git a/src/ios/AppDelegate+CDVXGPush.h b/src/ios/AppDelegate+CDVXGPush.h new file mode 100644 index 0000000..656d75b --- /dev/null +++ b/src/ios/AppDelegate+CDVXGPush.h @@ -0,0 +1,5 @@ +#import "AppDelegate.h" +#import + +@interface AppDelegate (CDVXGPush) +@end diff --git a/src/ios/AppDelegate+CDVXGPush.m b/src/ios/AppDelegate+CDVXGPush.m new file mode 100644 index 0000000..0ebdf1f --- /dev/null +++ b/src/ios/AppDelegate+CDVXGPush.m @@ -0,0 +1,13 @@ +#import "AppDelegate+CDVXGPush.h" + +@implementation AppDelegate (CDVXGPush) + +/** + * 追加推送事件 + */ +- (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { + NSLog(@"[AppDelegate] receive remote notification"); + [[NSNotificationCenter defaultCenter] postNotificationName: @"receivenotification" object:userInfo]; +} + +@end diff --git a/src/ios/CDVRegisterNotification.h b/src/ios/CDVRegisterNotification.h new file mode 100644 index 0000000..5edb5e6 --- /dev/null +++ b/src/ios/CDVRegisterNotification.h @@ -0,0 +1,9 @@ +#import + +@interface CDVRegisterNotification : NSObject + ++ (void) registerPush; ++ (void) registerPushForIOS8; ++ (void) registerNotification; + +@end diff --git a/src/ios/CDVRegisterNotification.m b/src/ios/CDVRegisterNotification.m new file mode 100644 index 0000000..cae1e42 --- /dev/null +++ b/src/ios/CDVRegisterNotification.m @@ -0,0 +1,72 @@ +#import "CDVRegisterNotification.h" + +@implementation CDVRegisterNotification + ++ (void) registerNotification +{ + NSLog(@"[CDVRegisterNotification] register notification"); +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ + // iOS8注册push方法 + float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue]; + if (sysVer < 8) { + [CDVRegisterNotification registerPush]; + } else { + [CDVRegisterNotification registerPushForIOS8]; + } +#else + // iOS8之前注册push方法 + // 注册Push服务,注册后才能收到推送 + [CDVRegisterNotification registerPush]; +#endif +} + ++ (void) registerPush +{ + NSLog(@"[CDVRegisterNotification] register under ios 8"); + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; +} + ++ (void) registerPushForIOS8 +{ +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ + NSLog(@"[CDVRegisterNotification] register ios 8"); + + //Types + UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; + + //Actions + UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init]; + + acceptAction.identifier = @"ACCEPT_IDENTIFIER"; + acceptAction.title = @"Accept"; + + acceptAction.activationMode = UIUserNotificationActivationModeForeground; + acceptAction.destructive = NO; + acceptAction.authenticationRequired = NO; + + //Categories + UIMutableUserNotificationCategory* inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; + + inviteCategory.identifier = @"INVITE_CATEGORY"; + + [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault]; + + [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal]; + + // using arc + // [acceptAction release]; + + NSSet* categories = [NSSet setWithObjects:inviteCategory, nil]; + + // using arc + // [inviteCategory release]; + + UIUserNotificationSettings* mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; + + [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; + + [[UIApplication sharedApplication] registerForRemoteNotifications]; +#endif +} + +@end diff --git a/src/ios/CDVXGPushPlugin.h b/src/ios/CDVXGPushPlugin.h new file mode 100644 index 0000000..4668ab5 --- /dev/null +++ b/src/ios/CDVXGPushPlugin.h @@ -0,0 +1,18 @@ +#import "CDVXGPushUtil.h" +#import +#import + +@interface CDVXGPushPlugin: CDVPlugin + +- (void) didRegisterForRemoteNotificationsWithDeviceToken:(NSNotification*)notification; +- (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; + +@end diff --git a/src/ios/CDVXGPushPlugin.m b/src/ios/CDVXGPushPlugin.m new file mode 100644 index 0000000..fac7fbd --- /dev/null +++ b/src/ios/CDVXGPushPlugin.m @@ -0,0 +1,127 @@ +#import "CDVXGPushPlugin.h" +#import "CDVXGPushUtil.h" + +@implementation CDVXGPushPlugin + +/** + * 插件初始化 + */ +- (void) pluginInitialize { + // 注册获取 token 回调 + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRegisterForRemoteNotificationsWithDeviceToken:) name:CDVRemoteNotification object:nil]; + + // 注册错误回调 + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFailToRegisterForRemoteNotificationsWithError:) name:CDVRemoteNotificationError object:nil]; + + // 注册接收回调 + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveRemoteNotification:) name:@"receivenotification" object:nil]; + + // 初始化 + self.util = [[CDVXGPushUtil alloc] init]; + self.callbackIds = [[NSMutableArray alloc] init]; + + // 启动 XGPush + [self.util startApp]; + [self.util initForReregister]; +} + +/** + * [description] + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (void) didRegisterForRemoteNotificationsWithDeviceToken:(NSNotification*)notification { + NSLog(@"[XGPushPlugin] receive device token: %@", notification.object); + [self.util registerDevice:notification.object]; +} + +/** + * [description] + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (void) didFailToRegisterForRemoteNotificationsWithError:(NSNotification*)notification { + NSLog(@"[XGPushPlugin] register fail"); +} + +/** + * [description] + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (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 = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:notification.object]; + [result setKeepCallback:[NSNumber numberWithBool:YES]]; + [self.commandDelegate sendPluginResult:result callbackId:callbackId]; + }]; +} + +/** + * [description] + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (void) registerpush:(CDVInvokedUrlCommand*)command { + NSString* alias = [command.arguments objectAtIndex:0]; + NSLog(@"[XGPushPlugin] registerpush: %@", alias); + + // FIXME: 放到 background thread 里运行时无法执行回调 + [self.util registerPush:alias successCallback:^{ + // 成功 + NSLog(@"[XGPushPlugin] registerpush success"); + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + + } errorCallback:^{ + // 失败 + NSLog(@"[XGPushPlugin] registerpush error"); + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + }]; +} + +/** + * [description] + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (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]; + + }]; +} + +/** + * [description] + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (void) addlistener:(CDVInvokedUrlCommand*)command { + NSLog(@"[XGPushPlugin] add listener: %@", command.callbackId); + [self.callbackIds addObject:command.callbackId]; +} + +@end diff --git a/src/ios/CDVXGPushUtil.h b/src/ios/CDVXGPushUtil.h new file mode 100644 index 0000000..5da1c0b --- /dev/null +++ b/src/ios/CDVXGPushUtil.h @@ -0,0 +1,17 @@ +#import + +typedef void (^CallbackBlock)(void); + +@interface CDVXGPushUtil: NSObject + +- (void) startApp; +- (void) initForReregister; +- (void) registerDevice:(NSData*)deviceToken; +- (void) registerPush:(NSString*)alias successCallback:(CallbackBlock)success errorCallback:(CallbackBlock)error; +- (void) unregisterPush:(CallbackBlock)success errorCallback:(CallbackBlock)error; +- (uint32_t) getAccessID; +- (NSString*) getAccessKey; + +@property NSData* deviceToken; + +@end diff --git a/src/ios/CDVXGPushUtil.m b/src/ios/CDVXGPushUtil.m new file mode 100644 index 0000000..ec89a7f --- /dev/null +++ b/src/ios/CDVXGPushUtil.m @@ -0,0 +1,84 @@ +#import "CDVXGPushUtil.h" +#import "CDVRegisterNotification.h" +#import "XGPush.h" + +@implementation CDVXGPushUtil + +/** + * 注册通知 + */ +- (void) initForReregister { + NSLog(@"[XGPush] init for re-register"); + [XGPush initForReregister:^{ + // 如果变成需要注册状态 + if (![XGPush isUnRegisterStatus]) { + [CDVRegisterNotification registerNotification]; + } + }]; +} + +/** + * 启动 xgpush + */ +- (void) startApp { + uint32_t accessID = [self getAccessID]; + NSString* accessKey = [self getAccessKey]; + NSLog(@"[XGPush] starting with access id: %u, access key: %@", accessID, accessKey); + [XGPush startApp:accessID appKey:accessKey]; +} + +/** + * 记录 device token + * + * @param {NSData*} deviceToken [description] + * @return {[type]} [description] + */ +- (void) registerDevice:(NSData*)deviceToken { + NSLog(@"[XGPush] register with token:%@", deviceToken); + self.deviceToken = deviceToken; +} + +/** + * 注册 xgpush + * + * @param {[type]} void [description] + * @return {[type]} [description] + */ +- (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 + * + * @param {[type]} uint32_t [description] + * @return {[type]} [description] + */ +- (uint32_t) getAccessID { + return [[[[NSBundle mainBundle] objectForInfoDictionaryKey:@"XGPushMeta"] valueForKey:@"AccessID"] intValue]; +} + +/** + * 获取 access key + * + * @param {[type]} NSString* [description] + * @return {[type]} [description] + */ +- (NSString*) getAccessKey { + return [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"XGPushMeta"] valueForKey:@"AccessKey"]; +} + +@end