Skip to content

Commit

Permalink
Merge pull request #35 from chabokpush/dev
Browse files Browse the repository at this point in the history
Dev to master for version v1.2.0
  • Loading branch information
amir-yaghoubi authored Dec 4, 2018
2 parents 9ce1c20 + c939344 commit 30dd6f4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ local.properties
**/ios/Pods/**
**/ios/ReactNativeFirebaseDemo.xcworkspace/
dist

package-lock\.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ public AdpPushClientModule(ReactApplicationContext reactContext) {
}

@ReactMethod
public void initializeApp(String appName, ReadableMap options, com.facebook.react.bridge.Callback callback) {

public void initializeApp(ReadableMap options, Promise promise) {
activityClass = getMainActivityClass();

try {
chabok = AdpPushClient.get();
} catch (Exception exc){
Log.d(TAG, "Chabok client not initialized");
}

if (chabok == null) {
chabok = AdpPushClient.init(
getReactApplicationContext(),
Expand All @@ -95,27 +101,30 @@ public void initializeApp(String appName, ReadableMap options, com.facebook.reac
options.getString("username"),
options.getString("password")
);

chabok.setDevelopment(options.getBoolean("isDev"));
chabok.enableDeliveryTopic();
attachChabokClient();
}
chabok.setDevelopment(options.getBoolean("devMode"));
chabok.addListener(this);
attachChabokClient();

if (activityClass != null) {
WritableMap response = Arguments.createMap();
response.putString("result", "success");
callback.invoke(response);
promise.resolve(response);
} else { // TODO improve sending error or mechanism
WritableMap response = Arguments.createMap();
response.putString("result", "failed");
callback.invoke(response);
promise.reject("500","Activity class is null", new IllegalArgumentException("Activity class is null"));
}
}

@ReactMethod
public void init(String appId, String apiKey, String username, String password, Promise promise) {

public void init(String appId, String apiKey, String username, String password, boolean devMode, Promise promise) {
activityClass = getMainActivityClass();

try {
chabok = AdpPushClient.get();
} catch (Exception exc){
Log.d(TAG, "Chabok client not initialized");
}

if (chabok == null) {
chabok = AdpPushClient.init(
getReactApplicationContext(),
Expand All @@ -125,10 +134,10 @@ public void init(String appId, String apiKey, String username, String password,
username,
password
);

chabok.addListener(getReactApplicationContext());
attachChabokClient();
}
chabok.setDevelopment(devMode);
chabok.addListener(this);
attachChabokClient();

if (activityClass != null) {
WritableMap response = Arguments.createMap();
Expand Down
14 changes: 14 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
## History

### v1.2.0 (2/12/2018)

#### Changes:

- Fix in changing Chabok environment.

#### Upgrade note:

- For change Chabok environments use `devMode` parameter in `init` method.
`init(APP_ID, API_KEY, USERNAME, PASSWORD, DEVMODE)`
- Remove `appName` parameter in `initializeApp` method.
`initializeApp(options)`
- The `setDevelopment` method is not available anymore. Use `devMode` parameter in `init` or `devMode` key in `initializeApp` method instead

### v1.1.1 (14/11/2018)
- Add `onSubscribe` and `onUnsubscribe` listener for getting subscribe and unsubscribe status.

Expand Down
58 changes: 32 additions & 26 deletions ios/AdpPushClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ @implementation AdpPushClient
apiKey:(NSString *) apiKey
username:(NSString *) username
password:(NSString *) password
devMode:(BOOL) devMode
promise:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

[PushClientManager setDevelopment:devMode];
NSArray *appIds = [appId componentsSeparatedByString:@"/"];
self.appId = appIds.firstObject;

BOOL state = [PushClientManager.defaultManager registerApplication:self.appId
apiKey:apiKey
userName:username
password:password];

if (state) {
RCTLogInfo(@"Initilized sucessfully");
resolve(@{@"result":@"Initilized sucessfully"});
Expand All @@ -50,34 +53,37 @@ @implementation AdpPushClient
}
[PushClientManager.defaultManager addDelegate:self];
[PushClientManager.defaultManager application:UIApplication.sharedApplication
didFinishLaunchingWithOptions:nil];
didFinishLaunchingWithOptions:nil];
}

RCT_EXPORT_METHOD(initializeApp:(NSString *) appName options:(NSDictionary *) options cbk:(RCTResponseSenderBlock) cbk) {
RCT_EXPORT_METHOD(initializeApp:(NSDictionary *) options
promise:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {

if(options == nil || [options isEqual:[NSNull null]]){
RCTLogInfo(@"Option parameter is null");
cbk(@[@{@"result":@"Option parameter is null"}]);
NSError *error = [[NSError alloc] initWithDomain:NSLocalizedDescriptionKey
code:400
userInfo:@{
@"result":@"Could not init chabok parameters"
}];
reject(@"400",@"Could not init chabok parameters",error);
} else {
BOOL devMode = [[options valueForKey:@"isDev"] boolValue];
[PushClientManager setDevelopment:devMode];

NSString *appId = [options valueForKey:@"appId"];
NSString *apiKey = [options valueForKey:@"apiKey"];
NSString *username = [options valueForKey:@"username"];
NSString *password = [options valueForKey:@"password"];

BOOL devMode = [[options valueForKey:@"devMode"] boolValue];
NSArray *appIds = [appId componentsSeparatedByString:@"/"];

[self init:appIds.firstObject
apiKey:apiKey
username:username
password:password
promise:^(id result) {
cbk(result);
} rejecter:^(NSString *code, NSString *message, NSError *error) {
cbk(@[@{@"error":message}]);
}];

devMode:devMode
promise:resolve
rejecter:reject];

}
}

Expand Down Expand Up @@ -120,14 +126,14 @@ @implementation AdpPushClient
}
BOOL state = [PushClientManager.defaultManager registerUser:userId
channels:chnl registrationHandler:^(BOOL isRegistered, NSString *userId, NSError *error) {
RCTLogInfo(@"isRegistered : %d userId : %@ error : %@",isRegistered, userId, error );
if (error) {
[self sendEventWithName:@"onRegister" body:@{@"error":error,
@"isRegister":@(NO)
}];
} else {
[self sendEventWithName:@"onRegister" body:@{@"isRegister":@(isRegistered)}];
}
RCTLogInfo(@"isRegistered : %d userId : %@ error : %@",isRegistered, userId, error );
if (error) {
[self sendEventWithName:@"onRegister" body:@{@"error":error,
@"isRegister":@(NO)
}];
} else {
[self sendEventWithName:@"onRegister" body:@{@"isRegister":@(isRegistered)}];
}
}];
if (state) {
RCTLogInfo(@"Registered to chabok with channels");
Expand Down Expand Up @@ -352,17 +358,17 @@ -(void) pushClientManagerDidChangedServerConnectionState {
}

-(void) pushClientManagerDidSubscribed:(NSString *)channel{
[self sendEventWithName:@"onSubscribe" body:@{@"name":channel}];
[self sendEventWithName:@"onSubscribe" body:@{@"name":channel}];
}
-(void) pushClientManagerDidFailInSubscribe:(NSError *)error{
[self sendEventWithName:@"onSubscribe" body:@{@"error":error}];
[self sendEventWithName:@"onSubscribe" body:@{@"error":error}];
}

-(void) pushClientManagerDidUnsubscribed:(NSString *)channel{
[self sendEventWithName:@"onUnsubscribe" body:@{@"name":channel}];
[self sendEventWithName:@"onUnsubscribe" body:@{@"name":channel}];
}
-(void) pushClientManagerDidFailInUnsubscribe:(NSError *)error{
[self sendEventWithName:@"onUnsubscribe" body:@{@"error":error}];
[self sendEventWithName:@"onUnsubscribe" body:@{@"error":error}];
}

- (void)invalidate {
Expand Down
37 changes: 25 additions & 12 deletions lib/modules/core/chabok.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ export const playServicesAvailability = AdpNativeModule.playServicesAvailability

export class AdpPushClient {

initializeApp = (appName, options, cbk) => {
AdpNativeModule.initializeApp(appName, options, response => {
cbk(response);
});
};
initializeApp = (options) => {
if (!options){
return Promise.reject(new Error('Options parameters is null, Please provide Chabok credential keys'));
}

return this.init(options.appId, options.apiKey, options.username, options.password, options.devMode);
};

init = async (appId, apiKey, username, password) => {
if (!appId || !apiKey || !username || !password) {
return Promise.reject(new Error('all parameters are required!'))
init = async (appId, apiKey, username, password, devMode) => {
if (!appId) {
return Promise.reject(new Error('appId parameters is required!'))
}
if (!apiKey) {
return Promise.reject(new Error('apiKey parameter is required!'))
}
if (!username) {
return Promise.reject(new Error('username parameter is required!'))
}
if (!password) {
return Promise.reject(new Error('password parameter is required!'))
}
if (typeof(devMode) !== "boolean") {
return Promise.reject(new Error('devMode parameter is required!'))
}

return await AdpNativeModule.init(appId, apiKey, username, password)
return await AdpNativeModule.init(appId, apiKey, username, password, devMode)
}

register = (userId, channels) => {
Expand Down Expand Up @@ -100,9 +113,9 @@ export class AdpPushClient {
AdpNativeModule.resetBadge()
}

setDevelopment = devMode => {
AdpNativeModule.setDevelopment(devMode)
}
// setDevelopment = devMode => {
// AdpNativeModule.setDevelopment(devMode)
// }


track = (trackName, data) => {
Expand Down

0 comments on commit 30dd6f4

Please sign in to comment.