From 0b7263559db2a3a1a0865040769368a093ec015a Mon Sep 17 00:00:00 2001 From: Ian Yu-Hsun Lin Date: Wed, 11 May 2016 12:51:54 +0800 Subject: [PATCH 1/2] export a constant indicates the app is launched by voip push notification --- index.js | 2 ++ .../RNVoipPushNotificationManager.m | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e54a9d8..f9608af 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,8 @@ var DEVICE_LOCAL_NOTIF_EVENT = 'voipLocalNotificationReceived'; export default class RNVoipPushNotification { + static wakeupByPush = (RNVoipPushNotificationManager.wakeupByPush === 'true'); + /** * Schedules the localNotification for immediate presentation. * diff --git a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m index e5bbd2d..8ef5362 100644 --- a/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m +++ b/ios/RNVoipPushNotification/RNVoipPushNotificationManager.m @@ -14,9 +14,28 @@ #import "RCTEventDispatcher.h" #import "RCTUtils.h" -NSString *const RNVoipRemoteNotificationsRegistered = @"VoipRemoteNotificationsRegistered"; -NSString *const RNVoipLocalNotificationReceived = @"VoipLocalNotificationReceived"; -NSString *const RNVoipRemoteNotificationReceived = @"VoipRemoteNotificationReceived"; +NSString *const RNVoipRemoteNotificationsRegistered = @"voipRemoteNotificationsRegistered"; +NSString *const RNVoipLocalNotificationReceived = @"voipLocalNotificationReceived"; +NSString *const RNVoipRemoteNotificationReceived = @"voipRemoteNotificationReceived"; + +static NSString *RCTCurrentAppBackgroundState() +{ + static NSDictionary *states; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + states = @{ + @(UIApplicationStateActive): @"active", + @(UIApplicationStateBackground): @"background", + @(UIApplicationStateInactive): @"inactive" + }; + }); + + if (RCTRunningInAppExtension()) { + return @"extension"; + } + + return states[@(RCTSharedApplication().applicationState)] ? : @"unknown"; +} @implementation RCTConvert (UILocalNotification) @@ -64,6 +83,13 @@ - (void)setBridge:(RCTBridge *)bridge object:nil]; } +- (NSDictionary *)constantsToExport +{ + NSString *currentState = RCTCurrentAppBackgroundState(); + NSLog(@"[RNVoipPushNotificationManager] constantsToExport currentState = %@", currentState); + return @{@"wakeupByPush": (currentState == @"background") ? @"true" : @"false"}; +} + - (void)registerUserNotification:(NSDictionary *)permissions { UIUserNotificationType types = UIUserNotificationTypeNone; From 3048679df21cd5a48b1dfadf42db4c1ce1c90d3b Mon Sep 17 00:00:00 2001 From: Ian Yu-Hsun Lin Date: Wed, 11 May 2016 13:04:52 +0800 Subject: [PATCH 2/2] update readme for exported constant `wakeupByPush` --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 875cb5a..ec050e1 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,24 @@ class MyComponent extends React.Component { // register your VoIP client, show local notification, etc. // e.g. this.doRegister(); + + /* there is a boolean constant exported by this module called + * + * wakeupByPush + * + * you can use this constant to distinguish the app is launched + * by VoIP push notification or not + * + * e.g. + */ + if (VoipPushNotification.wakeupByPush) { + // do something... + + // remember to set this static variable to false + // since the constant are exported only at initialization time + // and it will keep the same in the whole app + VoipPushNotification.wakeupByPush = false; + } /** * Local Notification Payload