Wednesday 1 April 2015

Simplest steps to integrate Parse for push notifications in Android

This blog entry is about how to fastly integrate Parse for push notifications in android. It only provide information on how to get started with the Parse's sample and the flow for most basic configurations.

Prerequisites : 

Signup into the www.parse.com. Create a project which will generate few keys like APPLICATION ID and CLIENT KEY to be used later in the project while integrating sdk.

Integration and Configuration:

1. Make a quickstart by navigating to this url https://www.parse.com/apps/quickstart
2. Select the Push options.
3. Select Android
4. Out of the two options : Create a new project and Add to existing project. Choose what you want to do ?

5. Choosing New Project will take you to https://www.parse.com/apps/quickstart#parse_push/android/new
Follow the steps on this page to make respective changes in the source files : Well you can find this step defined in the app properly

5.1 Download the the project and import it in the android studio.
5.2 We will need to connect to parse. This will require us to edit some of the files in the project.

5.2.1 Edit the Application class's onCreate() method

             public void onCreate() {
                // Replace the APPLICATION ID and CLIENT KEY with the one created for a project
                Parse.initialize(this, "APPLICATION ID", "CLIENT KEY");
             }

5.2.2 Add the following service and broadcast receiver definitions to AndroidManifest.xml immediately before the closing </application> tag:


<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
    android:exported="false">
  <intent-filter>
    <action android:name="com.parse.push.intent.RECEIVE" />
    <action android:name="com.parse.push.intent.DELETE" />
    <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
    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" />
 
    <!--
      IMPORTANT: Change "com.parse.starter" to match your app's package name.
    -->
    <category android:name="com.parse.starter" />
  </intent-filter>
</receiver>


Change the android:name attribute of <category> element above to match your application's package name.

Also add the permissions below, typically immediately before the opening <application> tag:


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 
<!--
  IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
  to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
    android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />


Change the android:name attribute of <category> element above to match your application's package name.

5.2.3 Compile and run.

6. Similarly follow all the substeps just as in step 5 to do the Parse integration in android sdk.


Testing for push notifications : 

OK, now you are done with the most basic integration so you are good to go for testing.

7. Navigate to https://www.parse.com/apps to open dashboard containing all your projects created till now. Select the current project and the select the "Push" option.

7.1 Now here you will find an option/button to send a push message. You can also see the notification history if any notifications send previously.
7.2 Once selected one can also find a box to create a message to be sent. And the settings to choose the devices that can receive the notifications.
7.3 Also you can update the time of delivery of notification
7.4 Once message is configured properly, it can be pushed to the devices.


8. If everything is fine, app will receive the notifications and on clicking notification it will launch the application with the launcher activity. 

Ok ,you are done here with the integration of Parse for push notifications in android. 
Wait .... We are missing something .... ?
Don't you want to use one of the icon for the notification from your app reserve rather than default icon ? I know you are dying to add .
Don't you want to an implementation specific to your app like an desired activity to be launched. Yes you always wanted to drive
So we will do it in our app by creating our app's version of class com.parse.ParsePushBroadcastReceiver which allows to customize notification and implementation on receiving push message.



public class MyReceiver extends ParsePushBroadcastReceiver {
  
     @Override
     protected Notification getNotification(Context context, Intent intent) {
         final Notification notification = super.getNotification(context, intent);
  // TODO Update this notification here          
         return notification;
     }
 
     @Override
     protected void onPushOpen(Context context, Intent intent) {
  //TODO For app specific implementation comment the following line calling super version of this method  
  // super.onPushOpen(context, intent);

  //TODO Add App Specific implementation below
         Intent lastIntent = new Intent(context,SecondActivity.class);
         lastIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(lastIntent);
     }
}



This is just a brief walk through of Parse for push notifications in android. It is a very big thing in itself. Parse is a collection of multiple sdks for various platform.

References: https://www.parse.com/

Source: https://github.com/Keshava11/parsecheck