Skip to content

Commit 0b86e8e

Browse files
Adding call to get the current device ID. Merge pull request #22 from Countly/dev-junaid
Get device Id and Author implemented and some text changes
2 parents 55d91e1 + 7acf0ba commit 0b86e8e

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

Countly.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Countly.setViewTracking = async function(enabled = true){
138138
/**
139139
*
140140
* Set Push notification messaging mode and callbacks for push notifications
141-
* Should be call before Countly init
141+
* Should be call after Countly init
142142
*/
143143
Countly.pushTokenType = function(tokenType, channelName, channelDescription){
144144
var args = [];
@@ -153,9 +153,20 @@ Countly.sendPushToken = function(options){
153153
args.push((options.messagingMode || "").toString());
154154
CountlyReactNative.sendPushToken(args);
155155
}
156+
157+
/**
158+
* This method will ask for permission, enables push notification and send push token to countly server.
159+
* Should be call after Countly init
160+
*/
156161
Countly.askForNotificationPermission = function(){
157162
CountlyReactNative.askForNotificationPermission([]);
158163
}
164+
165+
/**
166+
*
167+
* Set callback to receive push notifications
168+
* @param { callback listner } theListener
169+
*/
159170
Countly.registerForNotification = function(theListener){
160171
var event = eventEmitter.addListener('onCountlyPushNotification', theListener);
161172
CountlyReactNative.registerForNotification([]);
@@ -226,6 +237,20 @@ Countly.setLocation = function(countryCode, city, location, ipAddress){
226237
Countly.disableLocation = function(){
227238
CountlyReactNative.disableLocation();
228239
}
240+
/**
241+
*
242+
* Get currently used device Id.
243+
* Should be call after Countly init
244+
* */
245+
static Future<String> getCurrentDeviceId() async {
246+
if(!await Countly.isInitialized()) {
247+
console.warn('getCurrentDeviceId, init must be called before getCurrentDeviceId');
248+
return "init must be called before getCurrentDeviceId";
249+
}
250+
const result = await CountlyReactNative.getCurrentDeviceId();
251+
return result;
252+
}
253+
229254
Countly.changeDeviceId = function(newDeviceID, onServer){
230255
if(onServer === false){
231256
onServer = "0";
@@ -473,6 +498,11 @@ Countly.removeConsent = function(args){
473498
CountlyReactNative.removeConsent(features);
474499
}
475500

501+
/**
502+
*
503+
* Give consent for all features
504+
* Should be call after Countly init
505+
*/
476506
Countly.giveAllConsent = function(){
477507
CountlyReactNative.giveAllConsent();
478508
}

android/src/main/java/ly/count/android/sdk/react/CountlyReactNative.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,36 @@ public void hasBeenCalledOnStart(Promise promise){
153153
Boolean result = Countly.sharedInstance().hasBeenCalledOnStart();
154154
promise.resolve(result);
155155
}
156+
157+
@ReactMethod
158+
public void getCurrentDeviceId(ReadableArray args, final Callback myCallback){
159+
String deviceID = Countly.sharedInstance().getDeviceID();
160+
if (deviceID == null) {
161+
Log.d(Countly.TAG, "[CountlyReactNative] getCurrentDeviceId, deviceIdNotFound");
162+
myCallback.invoke("deviceIdNotFound");
163+
}
164+
else {
165+
Log.d(Countly.TAG, "[CountlyReactNative] getCurrentDeviceId: " + deviceID);
166+
myCallback.invoke(deviceID);
167+
}
168+
}
169+
170+
@ReactMethod
171+
public void getDeviceIdAuthor(ReadableArray args, final Callback myCallback){
172+
DeviceId.Type deviceIDType = Countly.sharedInstance().getDeviceIDType();
173+
if (deviceIDType == null) {
174+
Log.d(Countly.TAG, "[CountlyReactNative] getDeviceIdAuthor, deviceIdAuthorNotFound");
175+
myCallback.invoke("deviceIdAuthorNotFound");
176+
}
177+
else {
178+
Log.d(Countly.TAG, "[CountlyReactNative] getDeviceIdAuthor: " + deviceIDType);
179+
if(deviceIDType == DeviceId.Type.DEVELOPER_SUPPLIED){
180+
myCallback.success("developerProvided");
181+
}else{
182+
myCallback.success("sdkGenerated");
183+
}
184+
}
185+
}
156186

157187
@ReactMethod
158188
public void changeDeviceId(ReadableArray args){

example/Example.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ class Example extends Component {
5353
// Countly.setHttpPostForced(false); // Set to "true" if you want HTTP POST to be used for all requests
5454
Countly.enableApm(); // Enable APM features, which includes the recording of app start time.
5555
Countly.enableAttribution(); // Enable to measure your marketing campaign performance by attributing installs from specific campaigns.
56-
57-
Countly.setEventSendThreshold("10"); // Set event threshold value, Events get grouped together and are sent either every minute or after the unsent event count reaches a threshold. By default it is 10
58-
59-
/** Push Notification Settings */
6056

6157
await Countly.init("https://master.count.ly", "5b77e4c785410351f32d8aa286d2383195d13b93", "123456"); // Initialize the countly SDK.
6258

ios/src/CountlyReactNative.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ typedef void (^Result)(id _Nullable result);
1616
- (void)enableCrashReporting;
1717
- (void)addCrashLog:(NSArray*_Nullable)arguments;
1818

19+
- (void)getCurrentDeviceId:(NSArray*_Nullable)arguments callback:(RCTResponseSenderBlock _Nullable)callback;
20+
- (void)getDeviceIdAuthor:(NSArray*_Nullable)arguments callback:(RCTResponseSenderBlock _Nullable)callback;
1921
- (void)changeDeviceId:(NSArray*_Nullable)arguments;
2022
- (void)enableParameterTamperingProtection:(NSArray*_Nullable)arguments;
2123
- (void)pinnedCertificates:(NSArray*_Nullable)arguments;

ios/src/CountlyReactNative.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,28 @@ + (void) log: (NSString *) theMessage{
275275
// [Countly.sharedInstance suspend];
276276
}
277277

278+
RCT_EXPORT_METHOD(getCurrentDeviceId:(NSArray*)arguments callback:(RCTResponseSenderBlock)callback)
279+
{
280+
dispatch_async(dispatch_get_main_queue(), ^ {
281+
id value = [Countly.sharedInstance deviceID];
282+
if(value){
283+
callback(@[value]);
284+
}
285+
else{
286+
NSString *value = @"deviceIdNotFound";
287+
callback(@[value]);
288+
}
289+
});
290+
}
291+
292+
RCT_EXPORT_METHOD(getDeviceIdAuthor:(NSArray*)arguments callback:(RCTResponseSenderBlock)callback)
293+
{
294+
dispatch_async(dispatch_get_main_queue(), ^ {
295+
NSString *value = @"Not implemented for iOS";
296+
callback(@[value]);
297+
});
298+
}
299+
278300
RCT_EXPORT_METHOD(changeDeviceId:(NSArray*)arguments)
279301
{
280302
dispatch_async(dispatch_get_main_queue(), ^ {

0 commit comments

Comments
 (0)