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
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);
}
Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.
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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With