Code Monkey home page Code Monkey logo

vizury-android-sdk's Introduction

Vizury Logo

Summary

This is Android SDK integration guide.

Components

Example app

Examples on how the Vizury SDK can be integrated.

examples/HelloVizury is a sample app having a basic integration with vizury SDK.

Basic Integration

Getting the 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'

Manifest File Changes

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.

Vizury SDK Initialization

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
}

Event Logging

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(); 

Push Notifications

Vizury sends push notifications to Android devices using Firebase Cloud Messaging.

Enabling FCM

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.

Configuring Application

Add Firebase SDK

a) Place the google-services.json that you had downloaded from the FCM console in the app module.

add_google_services

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'

Manifest changes

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>

Additional configurations

Offline caching

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"/>

Controlling fcm registration and message handling

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
        	}
    	}
}

vizury-android-sdk's People

Contributors

anuragbhowmick avatar

Watchers

James Cloos avatar  avatar  avatar  avatar  avatar Somanath Reddy avatar  avatar  avatar  avatar Venkatesh Kumar avatar

vizury-android-sdk's Issues

Fcm not works in debug built but it works in release build

Hi Vizury,

We are implementing the Vizury App Push in our App. After using the code to control FCM registration or FCM message handling which is specified below. Normal Fcm Works only in release built but not in debug built. Can you suggest any solution.

Let's Gobumpr,
Android Architect,
Venkatesh

NullPointerException - onHandleIntent Action is null

I am getting following crash. It occurs only some times. I am not able to get exact scenario. Here is the full log details-

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
at com.vizury.mobile.Push.VizIntentService.onHandleIntent(SourceFile:30)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.