Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

How To Change Android Status Bar Notification

We will create a code for status bar notifications. First of all, we will create a project named 'Notifications', and then open its 'AndroidManifest.xml' file.

We are going to create a notification, it will be an image notification which is when dragged and clicked, it will result in a Google page.

So for opening the Google page, we will first provide the permission in our manifest file,

<uses-permission android:name=""android.permission.INTERNET""/>

After that we will design layout, for that we will open our 'activity_main.xml' file from our 'layout' folder, in which it is by default relative layout, who is the basic properties have been ended like height, width, padding etc...

Now in relative layout we will take two buttons, one to Create notification, and other to Cancel notification. Meaning from first button we can generate status bar notification, and with other button we can remove it. So,

<Button

android:layout_height=""wrap_content""

android:layout_width=""wrap_content""

android:text=""CREATE NOTIFICATION""

android:id=""@+id/button""

android:layout_alignparentTop=""true""

android:layout_alignparentRight=""true""

android:layout_alignparentLeft=""true""

android:layout_alignparentStart=""true""

/>

 

Now similarly we will create a Cancel notification button,

 

<Button

android:layout_height=""wrap_content""

android:layout_width=""wrap_content""

android:text=""CANCEL NOTIFICATION""

android:id=""@+id/button2""

android:layout_below=""@+id/button""

android:layout_alignRight=""@+id/button""

android:layout_alignEnd=""@+id/button""

android:layout_alignparentLeft=""true""

android:layout_alignparentRight=""true""

/>

 

Now we will design are main file inside 'java' folder, 'MainActivity.java', in which by default code is generated now, here we will do view identification of our two buttons,

b1=(Button)findViewById (R.id.button);

b2=(Button)findViewById (R.id.button2);

 

Now we will design ClickListener for both of the buttons,

 

b1.setOnClickListener(new View.onClickListener()

{

@Override

public void onClick(View v)

{

}

};

 

Now we will create a method sendNotification (),

 

public void sendNotification()

{

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

builder.setSmallIcon(android.R.drawable.ic_dialog_alert);

Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(""http://www.google.com/""));

PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

builder.setContentIntent(pendingIntent);

builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.wipmap.ic_launcher));

}

 

Now we will set some basic properties of it,

 

builder.setContentTitle(""Notification Title"");

builder.setContentText(""Your notification content here"");

builder.setSubText(""Tap to view the website"");

 

As 'notification' is a system service we need to use its method to generate it,

 

NotificationManager notificationmanager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 

To display notification in the notification bar,

notificationmanager.notify(1, builder.build());

 

Now in cancelnotification(), we want to write code for canceling the notification,

public void cancelNotification()

{

String ns=Context.NOTIFICATION_SERIVICE;

NotificationManager nMgr=(NotificationManager) getApplicationContext().getSystemService(ns);

nMgr.cancel(l);

}

Then we will execute our code, after executing we see that our code is running flawlessly.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere