Anuranjit Maindola
Anuranjit Maindola

Reputation: 1507

No notification displayed Android

This is my method that is called on click of a button.

public void onDisplayNotification(View v)
{
    Intent i = new Intent(this, Notification_Activity.class);
    i.putExtra("Code", "Notification Dismissed");
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
    NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
    nb.setContentTitle("Meeting").setContentText("In 5 minutes").setContentIntent(pendingIntent);
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Log.d("my","Hello");
    nm.notify(0, nb.build());
}

Nothing is displayed on button click. I checked the log cat and the method is running.

Upvotes: 1

Views: 189

Answers (4)

Harish Godara
Harish Godara

Reputation: 2386

Use something like this :

public void createNotification(View view) 
{
    // Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Build notification
    // Actions are just fake
    Notification noti = new Notification.Builder(this)
        .setContentTitle("New mail from " + "[email protected]")
        .setContentText("Subject").setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent)
        .addAction(R.drawable.icon, "Call", pIntent)
        .addAction(R.drawable.icon, "More", pIntent)
        .addAction(R.drawable.icon, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}

Upvotes: 1

imthegiga
imthegiga

Reputation: 1126

Well that nb.build() type method didn't work for me. I tried last month like this and it was working good:

   NotificationCompat.Builder builder = (new NotificationCompat.Builder(this)
            .setContentTitle("Hello Notification").setContentText("Notification Time!")
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent("your pending intent").setWhen(System.currentTimeMillis()));

   Notification notif = builder.getNotification();
        notif.defaults |= Notification.DEFAULT_SOUND;
        notif.flags |= Notification.FLAG_AUTO_CANCEL;
        notif.flags |= Notification.FLAG_SHOW_LIGHTS;

   NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify("put any id", notif);
        stopSelf();

Upvotes: -1

x90
x90

Reputation: 2170

please pay atention on android developers guide.

Required notification contents

A Notification object must contain the following:

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

Try to provide small icon resource for notification builder.

Upvotes: 2

APriya
APriya

Reputation: 2004

int NOTIFICATION_ID=1

    NotificationManager mNM = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
                    CharSequence NotificationTicket = "Test";
        CharSequence NotificationTitle = "Test";
        // specify the notification icon and time


        Notification notification = new Notification(R.drawable.icon,
                NotificationTicket, System.currentTimeMillis());
        Intent notificationIntent = new Intent();
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, NotificationTitle,
                NotificationContent, contentIntent);
        notification.flags |= Notification.FLAG_SHOW_LIGHTS
                | Notification.FLAG_AUTO_CANCEL;

        mNM.notify(NOTIFICATION_ID, notification);

Upvotes: 1

Related Questions