Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NotificationManager not accepting my notification

I am unable to get NotificationManager's Notifiy accept my arguments. The method is supposed to take 3 arguments.

String        tag
int           id
Notification  notification

enter image description here

For constructing the notification, I am using the NotificationCompat class, but I've tried even Notification.Builder.

import android.app.Notification;
import android.support.v4.app.NotificationCompat;

Regarding my build configuration, it is as follows:

compileSdkVersion 23
buildToolsVersion "23.0.2"

minSdkVersion 14
targetSdkVersion 21

EDIT: Code transcript:

import android.app.Notification;
import android.support.v4.app.NotificationCompat;

private void showNotification(Context context, String category, String title, String text, int tag, boolean ongoing)
{
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, new Random().nextInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(title);
    builder.setContentText(text);
    builder.setContentIntent(pendingIntent);
    builder.setOngoing(ongoing);
    builder.setSmallIcon(R.drawable.ic_launcher);

    Notification notification = builder.build();
    notificationManager.showNotification(context, category, title, text, new Random().nextInt(), ongoing);
    notificationManager.notify(null, 0, notification);
}
like image 420
Martin Rončka Avatar asked Apr 13 '16 08:04

Martin Rončka


People also ask

What is the purpose of the Notificationmanager class?

Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.

How do I use Notification Manager on Android?

Notification Manager. Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity. Because notifications can be very annoying, the user can disable notifications for each application.


2 Answers

Use the Compat version NotificationManagerCompat, initialize it notificationManager = NotificationManagerCompat.from(getApplicationContext());. And after that, notify the manager notificationManager.notify(null, 0, notification);

import android.support.v4.app.NotificationCompat;

...

private NotificationManagerCompat notificationManager;

...

// onCreate
notificationManager = NotificationManagerCompat.from(getApplicationContext());

...

// Somewhere in your code
Notification notification = new NotificationCompat.Builder(this)
    .setContentTitle("I'm a title")
    .setContentText("Some text")
    .setSmallIcon(R.drawable.ic_notification)
    .setContentIntent(pendingIntent)
    .build();

notificationManager.notify(null, 0, notification);
like image 112
Tom Sabel Avatar answered Sep 27 '22 22:09

Tom Sabel


notify in Object

this part of the 'error' message hints that your notificationManager is an Object, and that you declared it like this

Object notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE);

What you should have is this

NotificationManager notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
like image 21
Tim Avatar answered Sep 28 '22 00:09

Tim