Skip to content

Commit b259038

Browse files
Added support for clevertap push notifications for android platform
1 parent 71ead2b commit b259038

File tree

6 files changed

+122
-37
lines changed

6 files changed

+122
-37
lines changed

src/clevertap.android.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Common } from "./clevertap.common";
22
import { CleverTap as CleverTapInterface } from "./";
33
import * as utils from "tns-core-modules/utils/utils";
4+
import { enableLocation } from "./location-utils";
45

56
declare const com: any;
67
const CleverTapSdk = com.clevertap.android.sdk;
78
const CleverTapAPI = CleverTapSdk.CleverTapAPI;
89
const HashMap = java.util.HashMap;
910
const ArrayList = java.util.ArrayList;
10-
export class CleverTap extends Common implements CleverTapInterface {
1111

12+
export class CleverTap extends Common implements CleverTapInterface {
1213
profileGetProperty(propertyName: string) {
1314
return this.instance.getProperty(propertyName);
1415
}
@@ -43,6 +44,32 @@ export class CleverTap extends Common implements CleverTapInterface {
4344
public onUserLogin(profile) {
4445
this.instance.onUserLogin(getHashMap(profile));
4546
}
47+
48+
public async setLocation() {
49+
try {
50+
await enableLocation();
51+
this.instance.setLocation(this.instance.getLocation());
52+
} catch (error) {
53+
console.log("Error ", error);
54+
}
55+
}
56+
57+
public pushFcmRegistrationId(fcmRegId) {
58+
this.instance.pushFcmRegistrationId(fcmRegId, true);
59+
}
60+
61+
public handleMessage(message): boolean {
62+
const extras = new android.os.Bundle();
63+
Object.keys(message).forEach(key => {
64+
extras.putString(key, message[key]);
65+
});
66+
const info = CleverTapAPI.getNotificationInfo(extras);
67+
if (info.fromCleverTap) {
68+
CleverTapAPI.createNotification(utils.ad.getApplicationContext(), extras);
69+
return true;
70+
}
71+
return false;
72+
}
4673
}
4774

4875
export const cleverTap = new CleverTap();

src/clevertap.ios.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
1-
import { Common } from './clevertap.common';
1+
import { Common } from "./clevertap.common";
22
import { CleverTap as CleverTapInterface } from "./";
3-
import { on as onApplicationEvent, launchEvent } from "tns-core-modules/application";
3+
import {
4+
on as onApplicationEvent,
5+
launchEvent
6+
} from "tns-core-modules/application";
47

58
declare const CleverTap;
69

710
export class CleverTapImpl extends Common implements CleverTapInterface {
8-
9-
constructor() {
10-
super();
11-
onApplicationEvent(launchEvent, (args) => {
12-
CleverTap.autoIntegrate();
13-
});
14-
}
15-
16-
register() {
17-
throw new Error("Method not implemented.");
18-
}
19-
20-
updateProfile(profile: any) {
21-
CleverTap.sharedInstance().profilePush(profile);
22-
}
23-
24-
pushEvent(event: string, eventMeta: any) {
25-
CleverTap.sharedInstance().recordEventWithProps(event, eventMeta);
26-
}
27-
28-
pushChargedEvent(chargeDetails: any, items: any) {
29-
CleverTap.sharedInstance().recordChargedEventWithDetailsAndItems(chargeDetails, items);
30-
}
31-
32-
onUserLogin(profile: any) {
33-
CleverTap.sharedInstance().onUserLogin(profile);
34-
}
35-
36-
profileGetProperty(propertyName: string) {
37-
return CleverTap.sharedInstance().profileGet(propertyName);
38-
}
11+
constructor() {
12+
super();
13+
onApplicationEvent(launchEvent, args => {
14+
CleverTap.autoIntegrate();
15+
});
16+
}
17+
18+
register() {
19+
throw new Error("Method not implemented.");
20+
}
21+
22+
updateProfile(profile: any) {
23+
CleverTap.sharedInstance().profilePush(profile);
24+
}
25+
26+
pushEvent(event: string, eventMeta: any) {
27+
CleverTap.sharedInstance().recordEventWithProps(event, eventMeta);
28+
}
29+
30+
pushChargedEvent(chargeDetails: any, items: any) {
31+
CleverTap.sharedInstance().recordChargedEventWithDetailsAndItems(
32+
chargeDetails,
33+
items
34+
);
35+
}
36+
37+
onUserLogin(profile: any) {
38+
CleverTap.sharedInstance().onUserLogin(profile);
39+
}
40+
41+
profileGetProperty(propertyName: string) {
42+
return CleverTap.sharedInstance().profileGet(propertyName);
43+
}
44+
45+
public async setLocation() {
46+
throw new Error("Method not implemented.");
47+
}
48+
49+
public pushFcmRegistrationId(fcmRegId) {
50+
throw new Error("Method not implemented.");
51+
}
52+
53+
public handleMessage(message): boolean {
54+
throw new Error("Method not implemented.");
55+
}
3956
}
4057

4158
export const cleverTap = new CleverTapImpl();

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export interface CleverTap {
1010
onUserLogin(profile);
1111

1212
profileGetProperty(propertyName : string);
13+
14+
setLocation(): Promise<void>;
15+
16+
pushFcmRegistrationId(fcmRegId);
17+
18+
handleMessage(message): boolean;
1319
}
1420

1521
export const cleverTap: CleverTap;

src/location-utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isEnabled, enableLocationRequest } from "nativescript-geolocation";
2+
3+
export const enableLocation = async () => {
4+
try {
5+
if (!await isEnabled()) {
6+
await enableLocationRequest();
7+
return true;
8+
}
9+
return true;
10+
} catch (error) {
11+
return false;
12+
}
13+
};

src/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-clevertap",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Clevertap Nativescript Plugin.",
55
"main": "clevertap",
66
"typings": "index.d.ts",
@@ -52,6 +52,8 @@
5252
"tslint": "^5.11.0",
5353
"semver": "^5.5.0"
5454
},
55-
"dependencies": {},
55+
"dependencies": {
56+
"nativescript-geolocation": "^4.3.1"
57+
},
5658
"bootstrapper": "nativescript-plugin-seed"
5759
}

src/platforms/android/AndroidManifest.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
To read more about android permissions go to https://developer.android.com/guide/topics/permissions/index.html -->
66
<!--EXAMPLE: uses-permission android:name="android.permission.INTERNET"/> -->
77

8-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
8+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
9+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
910

11+
<application>
12+
13+
<receiver android:name="com.clevertap.android.sdk.CTPushNotificationReceiver" android:exported="false" android:enabled="true">
14+
</receiver>
15+
16+
<service android:name="com.clevertap.android.sdk.CTNotificationIntentService" android:exported="false">
17+
<intent-filter>
18+
<action android:name="com.clevertap.PUSH_EVENT"/>
19+
</intent-filter>
20+
</service>
21+
22+
<receiver android:name="com.clevertap.android.sdk.InstallReferrerBroadcastReceiver" android:exported="true">
23+
<intent-filter>
24+
<action android:name="com.android.vending.INSTALL_REFERRER"/>
25+
</intent-filter>
26+
</receiver>
27+
28+
<activity android:name="com.clevertap.android.sdk.InAppNotificationActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden"/>
29+
</application>
1030
</manifest>

0 commit comments

Comments
 (0)