Search code examples
javaandroidnotificationsbundleaction

How to create a notification with Action in android


I'm wondering how to create a notification in android with action icons that allowed me to call a method in the main activity.

just like the one in this image : Notification icon exemple


Solution

  • First Welcome to stackoverflow. I'd like to remind you this is not a website to learn how to program but a website to ask questions with actual problems that can help the community. Your questions have to be detailed and specific with your code or attempt as well as the error log.

    That being said, here is the best way to create a notification:

    Step 1 - Create Notification Builder

    First step is to create a notification builder using NotificationCompat.Builder.build(). You can use Notification Builder to set various Notification properties (small icons, large icons, title, priority etc)

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    

    Step 2 - Setting Notification Properties

    Once you have Builder object, you can set its Notification properties using Builder object as per your requirement. But this is mandatory to set at least following −

    • A small icon, set by setSmallIcon()
    • A title, set by setContentTitle()
    • Detail text, set by setContentText()

      mBuilder.setSmallIcon(R.drawable.notification_icon);
      
      mBuilder.setContentTitle("I'm a notification alert, Click Me!");
      
      mBuilder.setContentText("Hi, This is Android Notification Detail!");
      

    Step 3 - Attach Actions

    This is optional and only required if you want to attach an action with the notification. An action will allows users to go directly from the notification to an Activity in your application (where they can look at one or more events or do further work).

    The action is defined by a PendingIntent containing an Intent that starts an Activity in your application. To associate the PendingIntent with a gesture, call the appropriate method of NotificationCompat.Builder.

    For example, if you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().

    A PendingIntent object helps you to perform an action on your application's behalf, often at a later time, regardless whether or not your application is running.

    Also there's the stackBuilder object which will contain an artificial back stack for the started Activity. This ensures that navigating backward from the Activity leads out of your application to the Home screen.

    Intent resultIntent = new Intent(this, ResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =  stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    

    Step 4 - Issue the notification

    Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your notification. Make sure you call NotificationCompat.Builder.build() method on builder object before notifying it. This method combines all of the options that have been set and return a new Notification object.

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    // notificationID allows you to update the notification later on.
    mNotificationManager.notify(notificationID, mBuilder.build());
    

    I hope this answers your question.