From eb0595e5c08db47fa7bf6df1af8d43aaa7fd5f7d Mon Sep 17 00:00:00 2001 From: pikacode Date: Thu, 15 Dec 2016 14:51:15 +0800 Subject: [PATCH 1/2] iOS - add API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.增加延迟启动 API 2.增加 doc 说明 3.优化代码 --- doc/iOS_API.md | 27 +++++++++++++++++++++++++++ src/ios/Plugins/AppDelegate+JPush.h | 1 + src/ios/Plugins/AppDelegate+JPush.m | 27 +++++++++++++++++++++++++-- src/ios/Plugins/JPushDefine.h | 3 ++- src/ios/Plugins/JPushPlugin.h | 3 +++ src/ios/Plugins/JPushPlugin.m | 23 ++++++++++++++++------- src/ios/PushConfig.plist | 4 +++- www/JPushPlugin.js | 5 +++++ 8 files changed, 82 insertions(+), 11 deletions(-) diff --git a/doc/iOS_API.md b/doc/iOS_API.md index 4f5c6d9..88b9178 100644 --- a/doc/iOS_API.md +++ b/doc/iOS_API.md @@ -1,5 +1,6 @@ # iOS API +- [延迟注册和启动推送通知服务](#延迟注册和启动推送通知服务) - [开始与停止推送服务](#开始与停止推送服务) - [获取 RegistrationID](#获取-registrationid) - [别名与标签](#别名与标签) @@ -19,6 +20,32 @@ - [获取用户推送设置](#获取用户推送设置) - [监听事件统一说明](#监听事件统一说明) + + +## 延迟注册和启动推送通知服务 + +本插件默认在 App 启动完成之后,立即「注册苹果通知服务」+「启动 JPush SDK」,其中: + +- 「注册苹果通知服务」会弹出提示窗口向用户请求权限。 +- 「启动 JPush SDK」是使用 JPush 各项 API 的必要条件。 + +开发者可以根据自己的需求,延迟注册和启动,需做以下操作: + +1. 查找并配置 `PushConfig.plist` 文件中的 `Delay` 字段为 `YES`,表明会延迟使用,此时插件不再自动注册、启动通知。 +2. 在需要注册并启动通知的地方调用 API - startJPushSDK。 + +### API - startJPushSDK + +注册苹果通知服务,并启动 JPush SDK。 + +#### 接口定义 + +``` +window.plugins.jPushPlugin.startJPushSDK() +``` + + + ## 开始与停止推送服务 ### API - init diff --git a/src/ios/Plugins/AppDelegate+JPush.h b/src/ios/Plugins/AppDelegate+JPush.h index 05b1ad0..8072fa1 100644 --- a/src/ios/Plugins/AppDelegate+JPush.h +++ b/src/ios/Plugins/AppDelegate+JPush.h @@ -12,4 +12,5 @@ @interface AppDelegate (JPush) -(void)registerForRemoteNotification; +-(void)startJPushSDK; @end diff --git a/src/ios/Plugins/AppDelegate+JPush.m b/src/ios/Plugins/AppDelegate+JPush.m index e5f7d82..21cb7eb 100644 --- a/src/ios/Plugins/AppDelegate+JPush.m +++ b/src/ios/Plugins/AppDelegate+JPush.m @@ -28,6 +28,8 @@ return [self init_plus]; } +NSDictionary *_launchOptions; + -(void)applicationDidLaunch:(NSNotification *)notification{ if (notification) { if (notification.userInfo) { @@ -41,11 +43,25 @@ } } [JPUSHService setDebugMode]; - [self registerForRemoteNotification]; - [JPushPlugin setupJPushSDK:notification.userInfo]; + + NSString *plistPath = [[NSBundle mainBundle] pathForResource:JPushConfig_FileName ofType:@"plist"]; + NSMutableDictionary *plistData = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; + NSNumber *delay = [plistData valueForKey:JPushConfig_Delay]; + + _launchOptions = notification.userInfo; + + if (![delay boolValue]) { + [self startJPushSDK]; + } + } } +-(void)startJPushSDK{ + [self registerForRemoteNotification]; + [JPushPlugin setupJPushSDK:_launchOptions]; +} + -(void)registerForRemoteNotification{ if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { #ifdef NSFoundationVersionNumber_iOS_9_x_Max @@ -100,11 +116,18 @@ -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{ NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:notification.request.content.userInfo]; + + if ([SharedJPushPlugin respondsToSelector:@selector(jpushFireDocumentEvent:jsString:)]) { + + } [SharedJPushPlugin jpushFireDocumentEvent:JPushDocumentEvent_ReceiveNotification jsString:[userInfo toJsonString]]; completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); } -(void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ + if ([SharedJPushPlugin respondsToSelector:@selector(jpushFireDocumentEvent:jsString:)]) { + + } NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:response.notification.request.content.userInfo]; @try { [userInfo setValue:[response valueForKey:@"userText"] forKey:@"userText"]; diff --git a/src/ios/Plugins/JPushDefine.h b/src/ios/Plugins/JPushDefine.h index 3e58b11..06253a9 100644 --- a/src/ios/Plugins/JPushDefine.h +++ b/src/ios/Plugins/JPushDefine.h @@ -15,11 +15,12 @@ #define WEAK_SELF(weakSelf) __weak __typeof(&*self)weakSelf = self; +static NSString *const JPushConfig_FileName = @"PushConfig"; static NSString *const JPushConfig_Appkey = @"Appkey"; static NSString *const JPushConfig_Channel = @"Channel"; static NSString *const JPushConfig_IsProduction = @"IsProduction"; static NSString *const JPushConfig_IsIDFA = @"IsIDFA"; -static NSString *const JPushConfig_FileName = @"PushConfig"; +static NSString *const JPushConfig_Delay = @"Delay"; static NSString *const JPushDocumentEvent_ReceiveNotification = @"receiveNotification"; static NSString *const JPushDocumentEvent_OpenNotification = @"openNotification"; diff --git a/src/ios/Plugins/JPushPlugin.h b/src/ios/Plugins/JPushPlugin.h index 5bba719..1befbfd 100644 --- a/src/ios/Plugins/JPushPlugin.h +++ b/src/ios/Plugins/JPushPlugin.h @@ -12,6 +12,9 @@ } +//注册通知服务并启动 SDK +-(void)startJPushSDK:(CDVInvokedUrlCommand*)command; + //以下为js中可调用接口 //设置标签、别名 -(void)setTagsWithAlias:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/Plugins/JPushPlugin.m b/src/ios/Plugins/JPushPlugin.m index c1105c0..8f6f1b3 100644 --- a/src/ios/Plugins/JPushPlugin.m +++ b/src/ios/Plugins/JPushPlugin.m @@ -39,6 +39,10 @@ @implementation JPushPlugin +-(void)startJPushSDK:(CDVInvokedUrlCommand*)command{ + [(AppDelegate*)[UIApplication sharedApplication].delegate startJPushSDK]; +} + #pragma mark- 外部接口 -(void)stopPush:(CDVInvokedUrlCommand*)command{ [[UIApplication sharedApplication]unregisterForRemoteNotifications]; @@ -55,18 +59,13 @@ -(void)initial:(CDVInvokedUrlCommand*)command{ //do nithng,because Cordova plugin use lazy load mode. - SharedJPushPlugin = self; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(networkDidReceiveMessage:) - name:kJPFNetworkDidReceiveMessageNotification - object:nil]; } #ifdef __CORDOVA_4_0_0 - (void)pluginInitialize { NSLog(@"### pluginInitialize "); - SharedJPushPlugin = self; + [self initPlugin]; } #else @@ -75,12 +74,22 @@ NSLog(@"### initWithWebView "); if (self=[super initWithWebView:theWebView]) { } - SharedJPushPlugin = self; + [self initPlugin]; return self; } #endif +-(void)initPlugin{ + if (!SharedJPushPlugin) { + SharedJPushPlugin = self; + } + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(networkDidReceiveMessage:) + name:kJPFNetworkDidReceiveMessageNotification + object:nil]; +} + -(void)jpushFireDocumentEvent:(NSString*)eventName jsString:(NSString*)jsString{ dispatch_async(dispatch_get_main_queue(), ^{ [self.commandDelegate evalJs:[NSString stringWithFormat:@"cordova.fireDocumentEvent('jpush.%@',%@)", eventName, jsString]]; diff --git a/src/ios/PushConfig.plist b/src/ios/PushConfig.plist index 3dc78da..ed5d3e9 100644 --- a/src/ios/PushConfig.plist +++ b/src/ios/PushConfig.plist @@ -3,12 +3,14 @@ Appkey - + 1c29cb5814072b5b1f8ef829 Channel Subscription IsProduction IsIDFA + Delay + diff --git a/www/JPushPlugin.js b/www/JPushPlugin.js index a400df3..7bea916 100644 --- a/www/JPushPlugin.js +++ b/www/JPushPlugin.js @@ -49,6 +49,11 @@ JPushPlugin.prototype.isPushStopped = function (callback) { } // iOS methods + +JPushPlugin.prototype.startJPushSDK = function () { + this.call_native('startJPushSDK', [] , null) +} + JPushPlugin.prototype.setTagsWithAlias = function (tags, alias) { if (tags == null) { this.setAlias(alias) From 5d7181a659817f2b0514fc2b396aaa780c841a7b Mon Sep 17 00:00:00 2001 From: pikacode Date: Thu, 15 Dec 2016 16:01:51 +0800 Subject: [PATCH 2/2] update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3f25c68..bf6a719 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jpush-phonegap-plugin", - "version": "3.0.1", + "version": "3.0.2", "description": "JPush for cordova plugin", "cordova": { "id": "jpush-phonegap-plugin",