27 Sept 2015

Developing Notifications In android

Notification in android  is a special type of message which is used to show useful information when your app's UI is not visible to user.
Creating Notification is very simple, even you can create Notification by simply reading android Developer Guide.
Today I am will provide a ready to use code for creating notification for your app, Just copy this code and paste in your app code.
notification
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
 public void createNotification(){

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Following Three notification contents are *required 
     builder.setSmallIcon(R.drawable.notification_icon);
     builder.setContentTitle("Notification Title");
     builder.setContentText("Notification Details");

 // intent to start activity, provide Activity name where you want user to
  //to land when he/she clicks on notification, like: MainActivity

     Intent resultIntent = new Intent(this,MainActivity.class);

// Task builder to implement normal navigation 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
     stackBuilder.addParentStack(MainActivity.class);

     stackBuilder.addNextIntent(resultIntent);

     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
     builder.setContentIntent(resultPendingIntent);

     NotificationManager mNotifym = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// specify notification id,as first argument, i.e. I have used id=10
     mNotifym.notify(10, builder.build());


 }

 For Removing Notification use following code.
NotificationManager mNotifym = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifym.cancelAll();

This is a basic version of notification, you can add more feature by going through documentation. 
Did you find this post useful? Let Us know in the comments section below.

0 comments:

Post a Comment