This is Android SDK integration guide.
Examples on how the Vizury SDK can be integrated.
examples/HelloVizury
is a sample app having a basic integration with vizury SDK.
Vizury Android Library is available as a maven dependency. Add jcenter in repositories, in your top level build.gradle file
repositories {
jcenter()
}
Add the following dependency in your build.gradle file under app module
implementation 'com.vizury.mobile:VizurySDK:6.1.0'
Note : You may see the error 'All com.android.support libraries must use the exact same version specification' in the build.gradle of your app module. To resolve this, explicitly declare the version of the support libraties to use as shown below.
Open the build.gradle file of your app and find the dependencies block. Add the following line:
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:support-compat:28.0.0'
In the Package Explorer open the AndroidManifest.xml of your Android project and make the following changes
Add the below meta tags, replacing the place holders with associated values.
<meta-data
android:name="Vizury.VRM_ID"
android:value="{PACKAGE_ID}" />
<meta-data
android:name="Vizury.SERVER_URL"
android:value="{SERVER_URL}" />
<meta-data
android:name="Vizury.DEBUG_MODE"
android:value="{true/false}" />
PACKAGE_ID
and SERVER_URL
will be provided by the vizury account manager.
DEBUG_MODE
is used to show vizury debug/error/info logs. Set this to false when going to production.
In your MainActivity.java inside onCreate(Bundle SavedInstance) add following code snippet
import com.vizury.mobile.VizuryHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
//your code
VizuryHelper.getInstance(getApplicationContext()).init();
//your code
}
When a user browse through the app, various activities happen e.g. visiting a product, adding the product to cart, making purchase, etc. These are called events. Corresponding to each event, app needs to pass certain variables to the SDK which the SDK will automatically pass to Vizury servers.
Add the following code in each view where the event is to be tracked -
VizuryHelper.getInstance(context).logEvent(eventname, attributes);
Where
eventname : name of the event
attributes : AttributeBuilder object containing the attributes. You can set multiple attributes.
Eg.
import com.vizury.mobile.VizuryHelper;
AttributeBuilder builder = new AttributeBuilder.Builder()
.addAttribute("pid", "AFGEMSBBLL")
.addAttribute("quantity", "1")
.addAttribute("price", "876")
.addAttribute("category","clothing")
.build();
VizuryHelper.getInstance(context).logEvent("product page", builder);
You can also pass a JSONObject or a JSONArray as an attribute. For passing a JSONObject or JSONArray you can do something like this :
JSONObject attrs; // your attributes are in a JSONObject.
AttributeBuilder builder = new AttributeBuilder.Builder()
.addAttribute("productAttributes", attrs.toString())
.build();
Vizury sends push notifications to Android devices using Firebase Cloud Messaging.
Create a FCM[fcm_console] project if you dont already have one. After creating your project, click the Add App
button and select the option to add Firebase to your app. After this enter your app's package id. Follow the subsequent steps to download a google-services.json
file. After this click on the Cloud Messaging
Tab and note down the server key. This key has to be entered in the vizury dashboard.
a) Place the google-services.json
that you had downloaded from the FCM console in the app module.
b) Gradle changes
Project-level build.gradle (<project>/build.gradle
):
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.2.0'
}
}
App-level build.gradle (<project>/<app-module>/build.gradle
):
dependencies {
// Add this line
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
In the Package Explorer open the AndroidManifest.xml of your Android project and and make the following changes
Add the following meta-tags
<meta-data
android:name="Vizury.NOTIFICATION_ICON"
android:resource="{NOTIFICATION_ICON}" />
<meta-data
android:name="Vizury.NOTIFICATION_ICON_SMALL"
android:resource="{NOTIFICATION_ICON_SMALL}" />
NOTIFICATION_ICON
is the icon that comes at the left of a notification
NOTIFICATION_ICON_SMALL
is the icon that comes at the notification bar. This should be white icon on a transparent background
Refer Android Notifications and Style Icons for more details.
Add the following services for receiving the fcm token and messages.
<service
android:name="com.vizury.mobile.Push.VizFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.vizury.mobile.Push.VizFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Mandatory intent service for doing the heavy lifting
<service
android:name="com.vizury.mobile.Push.VizIntentService"
android:exported="false">
</service>
If your app supports offline features and you want to send the user behaviour data to vizury while he is offline, add the following tag in the manifest.
<meta-data
android:name="Vizury.DATA_CACHING"
android:value="true"/>
If you want to control FCM registration or FCM message handling then make these changes. In the Package Explorer open the AndroidManifest.xml of your Android project.
a) Remove the VizFirebaseMessagingService
and VizFirebaseInstanceIdService
from the manifest if added.
b) Make the following code changes
Pass the FCM token to vizury. Make sure that you call this everytime FCM Token is refreshed
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
VizuryHelper.getInstance(context).setGCMToken(refreshedToken);
...
}
On receving any push message call the vizury api for checking and parsing the payload
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message){
Map data = message.getData();
if (Utils.getInstance(getApplicationContext()).isPushFromVizury(data))
PushHandler.getInstance(getApplicationContext()).handleNotificationReceived(data);
else {
// your own logic goes here
}
}
}