Skip to content

Commit

Permalink
Merge pull request #33 from chabokpush/dev
Browse files Browse the repository at this point in the history
Dev to master version 1.1.0
  • Loading branch information
amir-yaghoubi authored Nov 12, 2018
2 parents b219d21 + f483ba6 commit e8f193f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 45 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ dependencies {
compile 'com.google.android.gms:play-services-gcm:10.2.6'

compile 'me.leolin:ShortcutBadger:1.1.22@aar'
compile 'com.adpdigital.push:chabok-lib:2.13.3'
compile 'com.adpdigital.push:chabok-lib:2.14.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Debug;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
Expand All @@ -23,6 +24,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.android.gms.common.ConnectionResult;
Expand All @@ -32,6 +34,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Console;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -159,11 +162,19 @@ public void run() {
response.putDouble("createdAt", msg.getCreatedAt());
response.putDouble("expireAt", msg.getExpireAt());
if (msg.getData() != null) {
response.putMap("data", toWritableMap(msg.getData()));
try {
response.putMap("data", toWritableMap(msg.getData()));
} catch (JSONException e) {
e.printStackTrace();
}
}

if (msg.getNotification() != null) {
response.putMap("notification", toWritableMap(msg.getNotification()));
try {
response.putMap("notification", toWritableMap(msg.getNotification()));
} catch (JSONException e) {
e.printStackTrace();
}
}

//sendEvent("onMessage", response);
Expand All @@ -181,35 +192,62 @@ public void run() {
response.putString("id", eventMessage.getId());
response.putString("eventName", eventMessage.getName());
response.putString("installationId", eventMessage.getInstallationId());
response.putMap("data", toWritableMap(eventMessage.getData()));
if (eventMessage.getData() != null) {
try {
response.putMap("data", toWritableMap(eventMessage.getData()));
} catch (JSONException e) {
e.printStackTrace();
}
}

sendEvent("onEvent", response);
}
});
}

WritableMap toWritableMap(JSONObject json) {
WritableMap response = Arguments.createMap();
Iterator iter = json.keys();
while (iter.hasNext()) {
String key = iter.next().toString();
try {
if (json.get(key) instanceof Integer) {
response.putInt(key, (Integer) json.get(key));
} else if (json.get(key) instanceof String) {
response.putString(key, (String) json.get(key));
} else if (json.get(key) instanceof JSONObject) {
response.putMap(key, toWritableMap((JSONObject) json.get(key)));
} else if (json.get(key) instanceof Double) {
response.putDouble(key, (Double) json.get(key));
} else if (json.get(key) instanceof Boolean) {
response.putBoolean(key, (Boolean) json.get(key));
}
} catch (JSONException e) {
// Something went wrong!
public static WritableMap toWritableMap(JSONObject jsonObject) throws JSONException {
WritableMap writableMap = Arguments.createMap();
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()) {
String key = (String) iterator.next();
Object value = jsonObject.get(key);
if (value instanceof Float || value instanceof Double) {
writableMap.putDouble(key, jsonObject.getDouble(key));
} else if (value instanceof Number) {
writableMap.putInt(key, jsonObject.getInt(key));
} else if (value instanceof String) {
writableMap.putString(key, jsonObject.getString(key));
} else if (value instanceof JSONObject) {
writableMap.putMap(key,toWritableMap(jsonObject.getJSONObject(key)));
} else if (value instanceof JSONArray){
writableMap.putArray(key, toWritableMap(jsonObject.getJSONArray(key)));
} else if (value == JSONObject.NULL){
writableMap.putNull(key);
}
}

return writableMap;
}

public static WritableArray toWritableMap(JSONArray jsonArray) throws JSONException {
WritableArray writableArray = Arguments.createArray();
for(int i=0; i < jsonArray.length(); i++) {
Object value = jsonArray.get(i);
if (value instanceof Float || value instanceof Double) {
writableArray.pushDouble(jsonArray.getDouble(i));
} else if (value instanceof Number) {
writableArray.pushInt(jsonArray.getInt(i));
} else if (value instanceof String) {
writableArray.pushString(jsonArray.getString(i));
} else if (value instanceof JSONObject) {
writableArray.pushMap(toWritableMap(jsonArray.getJSONObject(i)));
} else if (value instanceof JSONArray){
writableArray.pushArray(toWritableMap(jsonArray.getJSONArray(i)));
} else if (value == JSONObject.NULL){
writableArray.pushNull();
}
}
return response;
return writableArray;
}

private void attachChabokClient() {
Expand Down
7 changes: 6 additions & 1 deletion history.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
## History

### v1.1.0 (12/11/2018)
- Update Chabok android SDK version to [v2.14.0](https://github.com/chabokpush/chabok-client-android/releases/tag/v2.14.0)
- Update Chabok iOS SDK version to [v1.18.0](https://github.com/chabokpush/chabok-client-ios/releases/tag/v1.18.0)
- Fix promise reject for calling `getUserId` and `getInstallationId`

### v1.0.3 (10/11/2018)
- Update chabok android SDK version to v2.13.3
- Update chabok android SDK version to [v2.13.3](https://github.com/chabokpush/chabok-client-android/releases/tag/v2.13.3)

### v1.0.2 (6/11/2018)
- Update android bridge compileSdkVersion to 26
Expand Down
60 changes: 41 additions & 19 deletions ios/AdpPushClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,30 @@ @implementation AdpPushClient

RCT_EXPORT_METHOD(getInstallationId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSString *installationId = [PushClientManager.defaultManager getInstallationId];
resolve(installationId);
if (!installationId) {
NSError *error = [NSError.alloc initWithDomain:@"Not registered"
code:500
userInfo:@{
@"message":@"The installationId is null, You didn't register yet!"
}];
reject(@"500",@"The installationId is null, You didn't register yet!",error);
} else {
resolve(installationId);
}
}

RCT_EXPORT_METHOD(getUserId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSString *userId = [PushClientManager.defaultManager userId];
resolve(userId);
if (!userId) {
NSError *error = [NSError.alloc initWithDomain:@"Not registered"
code:500
userInfo:@{
@"message":@"The userId is null, You didn't register yet!"
}];
reject(@"500",@"The userId is null, You didn't register yet!",error);
} else {
resolve(userId);
}
}

#pragma mark - dev
Expand Down Expand Up @@ -231,7 +249,7 @@ @implementation AdpPushClient

RCT_EXPORT_METHOD(publishEvent:(NSString *) eventName data:(NSDictionary *) data resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
[PushClientManager.defaultManager publishEvent:eventName data:data];
[PushClientManager.defaultManager publishEvent:eventName data:data];
}

#pragma mark - subscribe
Expand All @@ -240,7 +258,7 @@ @implementation AdpPushClient
}

RCT_EXPORT_METHOD(subscribeEvent:(NSString *) eventName) {
[PushClientManager.defaultManager subscribeEvent:eventName];
[PushClientManager.defaultManager subscribeEvent:eventName];
}

RCT_EXPORT_METHOD(subscribeEvent:(NSString *) eventName installationId:(NSString *) installationId) {
Expand All @@ -257,7 +275,7 @@ @implementation AdpPushClient
}

RCT_EXPORT_METHOD(unSubscribeEvent:(NSString *) eventName) {
[PushClientManager.defaultManager unsubscribeEvent:eventName];
[PushClientManager.defaultManager unsubscribeEvent:eventName];
}

RCT_EXPORT_METHOD(unSubscribeEvent:(NSString *) eventName installationId:(NSString *) installationId) {
Expand Down Expand Up @@ -285,24 +303,28 @@ @implementation AdpPushClient

-(void) pushClientManagerDidReceivedMessage:(PushClientMessage *)message{
if (self.bridge) {
NSMutableDictionary *messageDict = [NSMutableDictionary.alloc initWithDictionary:[message toDict]];
[messageDict setObject:message.channel forKey:@"channel"];

[self sendEventWithName:@"onMessage" body:messageDict];
[self sendEventWithName:@"ChabokMessageReceived" body:messageDict];
NSMutableDictionary *messageDict = [NSMutableDictionary.alloc initWithDictionary:[message toDict]];
[messageDict setObject:message.channel forKey:@"channel"];
[self sendEventWithName:@"onMessage" body:messageDict];
[self sendEventWithName:@"ChabokMessageReceived" body:messageDict];
}
}

-(void) pushClientManagerDidReceivedEventMessage:(EventMessage *)eventMessage{
if (self.bridge) {
NSDictionary *event = @{
@"id":eventMessage.id,
@"installationId":eventMessage.deviceId,
@"eventName":eventMessage.eventName,
@"data":eventMessage.data
};
[self sendEventWithName:@"onEvent" body:event];
}
if (self.bridge) {
NSDictionary *eventMessageDic = @{
@"id":eventMessage.id,
@"installationId":eventMessage.deviceId,
@"eventName":eventMessage.eventName
};
NSMutableDictionary *eventPayload = [NSMutableDictionary.alloc initWithDictionary:eventMessageDic];
if (eventMessage.data) {
[eventPayload setObject:eventMessage.data forKey:@"data"];
}

[self sendEventWithName:@"onEvent" body:[eventPayload copy]];
}
}

-(void) pushClientManagerDidChangedServerConnectionState {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-chabok",
"version": "1.0.3",
"version": "1.1.0",
"description": "React native wrapper for Chabok SDK",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit e8f193f

Please sign in to comment.