Android Notifications Java Code - Android Studio

On this page we show you how to create a notification with Android. This article has nothing to do with a push Notification. Only a local notification is created. The code contains two classes. The MainActivity is the startpage of the app. When the button is pressed, the notification is created. The notification is displayed in the status bar and the ringtone is played. When the Notification is clicked, the Notifications-class opens with a Textview. Here could be a message for the user. It is also important to define the Notifications-class in the manifest.

Android Version API Version
4.0.3 15

First, two layout files must be created. For MainActivity, the layout contains only a button, and for the Notifications class, the layout contains only a TextView.

package com.aeqweb.notify;

import ...

public class MainActivity extends Activity {
    Button btn;

    protected void onCreate(Bundle savedInstanceState) {
        btn = (Button) findViewById(;
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Notify("Title", "You have pressed the button!");

    private void Notify(String title, String text) {
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
        Intent notificationIntent = new Intent(this, Notification.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

package com.aeqweb.notify;

import ...

public class Notification extends Activity{
    public void onCreate(Bundle savedInstanceState){
Info: This article has been partially translated automatically and may contain errors.

You Might Also Like


Social Media