Wednesday, 23 April 2014

Setting onClick listener for GCM notification

Firstly, there is no such thing like setOnClickListerner for GCM notification but you can combine your own intent with custom multiple actions.

Normally, you creates intent which is goint to pendingIntent which creates notification - and.. this intent is like nothing else simple activity.

This looks something like that:
  1. You set own action for intent, i.e create sms with custom text or call for custom number
  2. Then you create PendingIntent which is taking as one of few arguments our intent
  3. Next step is use Notification.Builder and set its behaviour for notification. You also add to it your pendingIntent(point 2) which will call action from intent (point 1)
  4. As a last step you build it and add to notify
So as you see, there is not place for such thing like onClick, but hey you can create own activity which will call in onCreate your action and then do own action like create sms or call to someone.

Here is my code:

 //gcm part, found on google start guid with gcm, modified some
 //my method takes some data stored in contentValues, this just an example  
 private void sendNotification(ContentValues cv) {  
      mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);  
   
      Intent intent = new Intent(this, StatPendingActivity.class);//CUSTOM ACTIVITY HERE  
   
      Bundle parcerable = new Bundle();  
      parcerable.putParcelable("data", cv);  
   
      intent.putExtra("parcelable", parcerable);  
   
      //intent drops into pendingIntent, REMEMBER USE FLAG_UPDATE_CURRENT to be able pass data!  
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
   
      NotificationCompat.Builder mBuilder =  
                new NotificationCompat.Builder(this)  
                          .setSmallIcon(android.R.drawable.presence_video_busy)  
                          .setLargeIcon(((BitmapDrawable) getResources().getDrawable(android.R.drawable.presence_video_busy)).getBitmap())  
                          .setContentTitle(cv.getAsString(key_title))  
                          .setStyle(new NotificationCompat.BigTextStyle()  
                                    .bigText(cv.getAsString(key_message)))  
                          .setContentText(cv.getAsString(key_message));  
   
      mBuilder.setContentIntent(contentIntent);  
   
      //FLAG TO SHOW ONLY ONE NOTIFICATION  
      Notification build = mBuilder.build();  
      build.flags |= Notification.FLAG_AUTO_CANCEL;  
      mNotificationManager.notify(NOTIFICATION_ID, build);  
 }  

And here is my custom Activity :

 public class StatPendingActivity extends Activity {  
   
   private String url = "http://localhost";  
   
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
   
           //this just an example, I takes some data from GCM which was in previous method  
     Bundle parcelable = getIntent().getBundleExtra("parcelable");  
     if (parcelable != null) {  
   
       ContentValues cv = parcelable.getParcelable("data");  
       Intent intent = new Intent();  
   
                //here I do some stuff with data from GCM  
       if (cv.getAsString(key_action).equals("CLICK_TO_SMS")) {  
         String smsNumber = cv.getAsString(key_sms_number);  
         String smsMessage = cv.getAsString(key_sms_message);  
   
         Uri uriNumber = Uri.parse("smsto:" + smsNumber);  
         intent = new Intent(Intent.ACTION_SENDTO, uriNumber);  
         intent.putExtra("sms_body", smsMessage);  
   
       }  
                //calling like notification some action, here is sending the sms  
                //we do not want remember this activity because it should behave like normal notification  
       intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
       startActivity(intent);  
   
                //my custom action, something like onClickListener  
                //this method call asynctask, so it wont be killed if activity is destroyed  
       sendClickStat(cv);//my action to do when I click notification  
   
       finish();//destroying this activity  
     }  
   }  
   
 }