Sunday, 23 December 2012

Open a new Activity on Receiving the notification in Android

Hello Droid Guys,

In this tutorial , I am going to explain how to open a new activity class
whenever a push notification comes on your device.

Step 1. For this first of all follow Android Goggle Cloud Messaging  . 
            In this blog I already explain how to register your application for GCM,
            Obtaining the sender id and getting the registration id from GCM server.


step 2. Once you complete the step-1 then inside your GCMIntentService.java class add
            following code.
      @Override  
      protected void onMessage(Context context, Intent arg1) {  
           Log.i(TAG, "new message= ");  
            String message = "success on server";  
         //  displayMessage(context, message);  
          // notifies user  
          generateNotification(context, message);  
      }  


Now,

    /**  
    * Issues a notification to inform the user that server has sent a message.  
    */  
   private static void generateNotification(Context context, String message) {  
     int icon = R.drawable.ic_launcher;  
     long when = System.currentTimeMillis();  
     NotificationManager notificationManager = (NotificationManager)  
         context.getSystemService(Context.NOTIFICATION_SERVICE);  
     Notification notification = new Notification(icon, message, when);  
     String title = context.getString(R.string.app_name);  
     Intent notificationIntent = new Intent(context, Messenger.class);  
     notificationIntent.putExtra("MESSAGE",message);  
     notificationIntent.putExtra("ISNOTIFICATION",true);  
     // set intent so it does not start a new activity  
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  
         Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);  
     PendingIntent intent =  
         PendingIntent.getActivity(context, 0, notificationIntent, 0);  
     notification.setLatestEventInfo(context, title, message, intent);  
     notification.flags |= Notification.FLAG_AUTO_CANCEL;  
     notificationManager.notify(0, notification);  
   }  



This , will helps you to open a new class whenever apush notification comes.
On tapping the Notification from notification bar.

Enjoy Coding... :)
Cheers :)

Also see this link for help
      1. Google Cloud Messaging 

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

6 comments:

  1. hi buddy, but i think we need to click on the notification in notification bar..Is there way to do it without clicking the notifcation, but just automatically, immediately on receiving push notification ?

    ReplyDelete
  2. Hello AlvajIndia,

    Inside onMessage() method you have to call
    Intent notificationIntent = new Intent(context, Messenger.class);
    notificationIntent.putExtra("MESSAGE",message);
    notificationIntent.putExtra("ISNOTIFICATION",true);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent =
    PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);

    then might be it will works.

    ReplyDelete
  3. Hi Mukesh,

    My notification will open the activity as given PendingIntent but for multiple notification its keep opening the same activity again and again, can you help me to solve this problem ?

    ReplyDelete
  4. Hello Gopal,
    You mean the notification msg remain same evrey time??
    Or
    You have more than one notification in your app??

    ReplyDelete
  5. Hi Mukesh i have same problem...as gopal..
    Suppose i receive 1st notification and click notification and it open application activity...
    Now i receive 2nd notification and i open it again but here not its showing its previous state of 1st notification it is not updating that activity... i m calling same activity with different intent extras... it should change according to what i receive in intent but it is not changing..

    Pleae help

    ReplyDelete
  6. Hello Rina,
    Yes, that is an issue with push notification Here is the solution which I am doing
    and Its working for me: Add following code in your calling activity class

    /*
    * fix for the bug: when new push will be receive
    * the intent will showing old push data.
    * Note: when we are using clear top and single top and want to get new data from intent
    * then we use onNewIntent(Intent intent)
    */
    @Override
    protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    Bundle extras = intent.getExtras();

    if(null != extras) {

    //do your operation here
    //then clear the extra
    extras.clear();
    extras=null;

    }
    }

    ReplyDelete

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin