Showing posts with label launch activity when push come in android. Show all posts
Showing posts with label launch activity when push come in android. Show all posts

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 

 

Copyright @ 2013 Android Developers Blog.