Upload
mobi-fly
View
84
Download
1
Embed Size (px)
Citation preview
Mixpanel Integration with Android
BrainBox Network. Copyright@2015. All rights reserved
The document provides steps to integrate Android with Mixpanel integration. The integration process requires following steps ‐
1. Instructions 2. Create New Project 3. Dashboard Setup 4. Configuration of Project 5. Android Code Setup 6. Custom Notification 7. Send Image with Push Notification 8. Further Information
BrainBox Network. Copyright@2015. All rights reserved
INSTRUCTIONS
➔ Please Follow these instructions: ➔ Create Google Account ➔ Open below given Url:
https://console.developers.google.com ➔ Click on “Select Project” dropdown.
BrainBox Network. Copyright@2015. All rights reserved
CREATE NEW PROJECT
➔ Give name of new project in the “Project” field. ➔ Click on “Enable and Manage API” button. ➔ Enable these services by “Google Cloud Messaging” ➔ Click on “Credentials tag” (In the left side or below of overview) ➔ Click on “Create Credentials” ➔ Select “API key” ➔ Then select android key ➔ Select “+Add Package name and fingerprint” ➔ Enter your “Package name” ➔ Enter your “SHA‐1 Key”
1. Open CMD from your system Java bin directory For Ex: “C:\Program Files (x86)\Java\jdk1.6.0\bin”
2. Run following command in CMD 3. Keytool ‐list ‐v ‐keystore
"C:\Users\Admin\.android\debug.keystore" (Mention your system path till “debug.keystore” file)
4. ‐alias androiddebugkey ‐storepass android ‐keypass android 5. Hence SHA‐1 KEY will shown in output
6. After fill all information Select Create & Save android key in somewhere.
BrainBox Network. Copyright@2015. All rights reserved
➔ Again Click on “Create Credentials” ➔ Select “API key” ➔ Then select Server key ➔ Then click Create Button
BrainBox Network. Copyright@2015. All rights reserved
➔ Open below link on browser https://developers.google.com/mobile/add
➔ Pick a platform (Android / IOS) ➔ Select your App name ➔ Write your “Package name” in android package name box ➔ Select “CONTINUE TO Choose and configure service” ➔ Adding services which you wants ➔ For push notification enable cloud messaging ➔ Select continue to generate configuration files
BrainBox Network. Copyright@2015. All rights reserved
➔ Click on “Download google‐services.json” ➔ After downloading, copy the google‐services.json to the app/ or mobile/module directory in
your Android project
BrainBox Network. Copyright@2015. All rights reserved
DASHBOARD SETUP
➔ Open Mixpanel website and Create new account or Login ➔ Click on “Create Project” ➔ Write your “Project name” then on “Create Project” button ➔ Click on “Create Project”
BrainBox Network. Copyright@2015. All rights reserved
CONFIGURATION OF PROJECT
➔ Click on Settings Icon, showing in the left bottom corner.
➔ Save Project Token, for use it further
BrainBox Network. Copyright@2015. All rights reserved
➔ Click on Notifications Icon ➔ Paste your Android GCM Service key ➔ Click on “Save Changes” ➔ Now click on “Close”
BrainBox Network. Copyright@2015. All rights reserved
ANDROID CODE SETUP
➔ Add dependencies to App/build.gradle: Compile 'com.google.android.gms:play‐services:8.4.0' Compile 'com.mixpanel.android:mixpanel‐android:4.8.0'
BrainBox Network. Copyright@2015. All rights reserved
➔ Add permissions to app/src/main/AndroidManifest.xml <uses‐permission android:name="android.permission.INTERNET" /> <uses‐permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses‐permission android:name="android.permission.GET_ACCOUNTS" /> <uses‐permission android:name="android.permission.WAKE_LOCK" /> <permission android:name=“<YOUR_APPLICATION_PACKAGE>.permission.C2D_MESSAGE"
android:protectionLevel="signature" /> <uses‐permission android:name="<YOUR_APPLICATION_PACKAGE>.permission.C2D_MESSAGE" />
BrainBox Network. Copyright@2015. All rights reserved
➔ Paste below code in “Application” tag in manifest <receiver android:name="com.mixpanel.android.mpmetrics.GCMReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent‐filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="YOUR_PACKAGE_NAME" /> </intent‐filter> </receiver>
BrainBox Network. Copyright@2015. All rights reserved
➔ Setup Java Code in App
MixpanelAPI mixpanel = MixpanelAPI.getInstance(this,<MIXPANEL TOKEN>"); mixpanel.identify(GENERATE DISTINCT ID); MixpanelAPI.People people = mixpanel.getPeople(); people.identify(SET_USER_ID); people.initPushHandling(GOOGLE_PROJECT_NUMBER); people.showNotificationIfAvailable(this);
BrainBox Network. Copyright@2015. All rights reserved
➔ Setup User Profile in Mixpanel JSONObject messageProps = new JSONObject(); try { messageProps.put("User Age","25"); } catch (JSONException e) { e.printStackTrace(); } mixpanel.registerSuperPropertiesOnce(messageProps);
OR mixpanel.registerSuperProperties(messageProps);
BrainBox Network. Copyright@2015. All rights reserved
➔ Set Super Properties try { final JSONObject userProfile = new JSONObject(); userProfile.put("$name" ,"TEST"); userProfile.put("$email" , "[email protected]"); String timeStamp = new SimpleDateFormat("yyyy‐MM‐dd HH:mm:ss").format(new Date()); userProfile.put("$created", timeStamp); mixpanel.getPeople().set(userProfile); } catch (JSONException e) { e.printStackTrace(); }
BrainBox Network. Copyright@2015. All rights reserved
➔ Tracking User Action mixpanel.track(“EVENT_NAME"); WRITE BELOW CODE IN ACTIVITY @Override protected void onDestroy() { super.onDestroy(); mixpanel.flush(); }
BrainBox Network. Copyright@2015. All rights reserved
CUSTOM NOTIFICATION
➔ CREATE OWN RECEIVER (For Ex. My Receiver) : ➔ Insert Below code in app/src/main/AndroidManifest.xml
<receiver android:name=“.My Receiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent‐filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> <category android:name="com.google.android.gcm.demo.app"/> </intent‐filter> <intent‐filter> <action android:name=“<GIVEN NAME (MYBROADCAST)>"> </action> </intent‐filter> </receiver>
BrainBox Network. Copyright@2015. All rights reserved
➔ MYRECEIVER.JAVA class CODE
public class MyReceiver extends BroadcastReceiver { Context context; @Override public void onReceive(Context context, Intent intent) { Bundle data = intent.getExtras(); String notificationMessage = data.getString("mp_message"); this.context=context; if(data.containsKey("IMAGE_KEY")) {
//NOTIFICATION WITH IMAGE String image_url = data.getString("IMAGE_URL"); new DownloadImage().execute(image_url,notificationMessage);
} else
{ //NOTIFICATION WITHOUT IMAGE simpleNotification(notificationMessage); }
} public class DownloadImage extends AsyncTask<String,Void,Bitmap> { Bitmap message_bitmap = null; @Override protected Bitmap doInBackground(String... parms) { try { URL url = new URL(parms[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); message_bitmap = BitmapFactory.decodeStream(input); int icon = R.mipmap.ic_launcher; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); String title = "<APP_TITLE>"; Intent notificationIntent = null; notificationIntent = new Intent(Intent.ACTION_VIEW); notificationIntent.addCategory(Intent.CATEGORY_BROWSABLE); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
BrainBox Network. Copyright@2015. All rights reserved
Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent1 = PendingIntent.getActivity(context, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(context) .setContentTitle(title) .setContentText(parms[1]) .setContentIntent(intent1) .setSmallIcon(icon) .setWhen(when) .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(message_bitmap)) .build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification); } catch (Exception e) { e.printStackTrace(); } return message_bitmap; } @Override protected void onPostExecute(Bitmap aVoid) { super.onPostExecute(aVoid); } } public void simpleNotification(String message)
{ int icon = R.mipmap.ic_launcher; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE); String title = "<APP_TITLE>"; Intent notificationIntent = null; notificationIntent = new Intent(Intent.ACTION_VIEW); notificationIntent.addCategory(Intent.CATEGORY_BROWSABLE); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent1 =PendingIntent.getActivity(context, 0,
notificationIntent, 0); Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent1)
.setSmallIcon(icon)
.setWhen(when)
.build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification);
}
BrainBox Network. Copyright@2015. All rights reserved
}
SEND IMAGE WITH PUSH NOTIFICATION FROM MIXPANEL DASHBOARD
➔ Create Push Notification in Mixpanel ➔ Set Audience ➔ Set your Push Notification ➔ Set Custom data (Extra data) ➔ For Image send Image_key
For Eg: Image_key ‐> “12345”) and Image_url ‐> “http://tineye.com/images/widgets/mona.jpg”
➔ In Custom data ‐> In Json form‐>Send
BrainBox Network. Copyright@2015. All rights reserved
FOR FURTHER INFORMATION
➔ https://mixpanel.com/help/reference/android ➔ https://github.com/mixpanel/mixpanel‐android
BrainBox Network. Copyright@2015. All rights reserved
Thank You So Very Much
For queries please write on [email protected] Developed By Mohit Saini Android Developer ‐ Mobifly mobifly.in
BrainBox Network. Copyright@2015. All rights reserved