Notification:
A notification is a message that is used as an alert outside the application.When you call notification, it first shows an icon in notification area. From Lollipop (API 5.0) Google modified in the notification area. when you get the notification, only icon will show in status bar (top most area) .If you want to show the details of notification you have to scroll down the notification drawer.
Notifications are handled by Notification Manager in android. Notification Manager is a system service used to manage notifications.
Creating of Notification :
To create the notification, You should use NotificationCompat and its subclasses, particularly
Create a Notification :
When creating a notification, specify the UI content and action with a NotificationCompat.Builder object. A Builder object must include the following:
- A small icon, set by setSmallIcon()
- A title, set by setContentTitle()
- Detail text, set by setContentText()
For example:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Hello World!");
Action in Notification:
Action in notification is an optional part. You can set only one action in notification.If you want to perform some operation in notification, you have to set action. Action will take you in your application activity where you show the event which causes notification generated. In Notification action set inside the pendingIntent which contains Intent that start an Activity.
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Fire Notification
- Create an instance of
NotificationManager
. - notify() method used to fire the notification. When you call notify(), specify a notification ID, you can use this ID to update the notification.
- Call build() method, which returns a Notification object containing your specifications
NotificationCompat.Builder mBuilder; // Sets an ID for the notification int mNotificationId = 1; // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build());
In the next tutorial we will discuss about custom notification.
project
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="musicplayer.com.androidnotification.MainActivity"> | |
<Button | |
android:id="@+id/btnNoti" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Create Notification" /> | |
<Button | |
android:id="@+id/btnCancelNoti" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/btnNoti" | |
android:layout_marginTop="10dp" | |
android:text="Cancel Notification" /> | |
</RelativeLayout> |
MainActivity.Java
| ||
import android.content.Intent; | ||
import android.support.v4.app.NotificationCompat; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
public class MainActivity extends AppCompatActivity { | ||
NotificationCompat.Builder mBuilder; | ||
NotificationManager mNotifyMgr; | ||
// Set the notification id to identify | ||
int mNotificationId = 100; | ||
Button btnNoti,btnCancelNoti; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
btnNoti = (Button)findViewById(R.id.btnNoti); | ||
btnCancelNoti =(Button)findViewById(R.id.btnCancelNoti); | ||
btnNoti.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
createNotification(); | ||
} | ||
}); | ||
btnCancelNoti.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
cancelNotification(); | ||
} | ||
}); | ||
} | ||
private void createNotification(){ | ||
mBuilder = | ||
new NotificationCompat.Builder(this) | ||
.setSmallIcon(R.mipmap.ic_launcher) | ||
//set title here | ||
.setContentTitle("My Notification") | ||
//set the text | ||
.setContentText("This is my first notification."); | ||
// set the pendingintent to come on that activity that you set in Intent. | ||
Intent intent = new Intent(this, MainActivity.class); | ||
PendingIntent pendingIntent = | ||
PendingIntent.getActivity( | ||
this, | ||
0, | ||
intent, | ||
PendingIntent.FLAG_UPDATE_CURRENT); | ||
mBuilder.setContentIntent(pendingIntent); | ||
// Gets an instance of the NotificationManager service | ||
mNotifyMgr = | ||
(NotificationManager) getSystemService(NOTIFICATION_SERVICE); | ||
// Builds the notification and issues it. | ||
mNotifyMgr.notify(mNotificationId, mBuilder.build()); | ||
} | ||
private void cancelNotification(){ | ||
if(mNotifyMgr != null){ | ||
mNotifyMgr.cancel(mNotificationId); | ||
} | ||
} | ||
} |
Screenshot
0 comments:
Post a Comment