Creating a simple Notification
suggest changeThis example shows how to create a simple notification that starts an application when the user clicks it.
Specify the notification’s content:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) // notification icon .setContentTitle("Simple notification") // title .setContentText("Hello word") // body message .setAutoCancel(true); // clear notification when clicked
Create the intent to fire on click:
Intent intent = new Intent(this, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); mBuilder.setContentIntent(pi);
Finally, build the notification and show it
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build());
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents