Showing posts with label BroadcastReceiver. Show all posts
Showing posts with label BroadcastReceiver. Show all posts

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

Monday, 1 December 2014

Resolving Last ShutDown State (Normal or Abrupt)

This blog post is about how to resolve if the last device shutdown happened normally or abruptly.

Actually the problem with which this post was started was to check in an application whether the last shutdown happened abruptly with a battery detachment.

But since there is no broadcast intent available to check for the  battery detachments. It means there is no direct way to deduce whether the last shutdown was caused by detaching battery from the device.

So we started to reduce the problem firstly to check if the last state was simply normal or abrupt. And then in later posts if possible finding the reason for the abrupt shutdown was due to the detaching of battery.

The two parts are :
1. First part mainly deals with the checking of the app that whether the last shutdown was successful or not.
2. Second part mainly deals with checking if last shutdown was abrupt and was due to battery detachment

Here in this post we will only deal with the first part.

Important point :
This investigative implementation is totally based upon the fact that the intent with action ACTION_SHUTDOWN is never called by the system before going dead when it happens abruptly.

We can accomplish the first problem of resolving reason of shutdown by implementing BroadcastReceiver that listens for two broadcast intents with actions namely ACTION_SHUTDOWN and BOOT_COMPLETED.

We will be achieving our this task by saving a value (currently in Preferences) when system sends broadcast for ACTION_SHUTDOWN and checking its value when device is rebooted and system send a broadcast for BOOT_COMPLETED.

Implemented classes are :

BroadcastReceiver

public class DisconnectReceiver extends BroadcastReceiver
{
 @Override
 public void onReceive(Context context, Intent intent)
 {
  if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
  {
   int shutDownCode = PreferenceManager.getDefaultSharedPreferences(DroidApplication.getsContext()).getInt(
     DisconnectConstants.KEY_NORMAL, 0);

   // Checking for the last shutdown value (1 = Normal ShutDown and 0 = Abrupt ShutDown )
   if (shutDownCode == 1)
   {
    // Reset the last value
    Editor editor = PreferenceManager.getDefaultSharedPreferences(DroidApplication.getsContext()).edit();
    editor.putInt(DisconnectConstants.KEY_NORMAL, 0);
    editor.commit();

    // TODO Here goes the user code to be implemented 
    
   }
   else if (shutDownCode == 0)
   {
    // TODO Here goes the user code to be implemented 
    
   }
  }
  else if (intent.getAction().equalsIgnoreCase(Intent.ACTION_SHUTDOWN))
  {
   // Saving value in Preferences for ShutDown broadcast
   Editor editor = PreferenceManager.getDefaultSharedPreferences(DroidApplication.getsContext()).edit();
   editor.putInt(DisconnectConstants.KEY_NORMAL, 1);
   editor.commit();
  }
 }
}

Changes in AndroidManifest.xml

Under <manifest> tag

<!-- Permission for getting RECEIVE_BOOT_COMPLETED -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Under <application> tag

<!-- Receiver to checks for broadcast for ACTIONs : ACTION_SHUTDOWN and ACTION_BOOT_COMPLETED -->
 <receiver android:name="com.droidutility.disconnect.utils.DisconnectReceiver" >
  <intent-filter>
   <action android:name="android.intent.action.ACTION_SHUTDOWN" />
   <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
 </receiver>


 
 
Please check here the code added in a library project with usage instructions in README.md.