Music
Music

Reputation: 29

How to send an android notification using Notification

I tried to send a notification using this block of code, but some methods are deprecated and some are unavailable. Please provide a solution.

 Intent intent= new Intent();
 PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);
 Notification.Builder notification= new Notification.Builder(MainActivity.this)
         .setTicker("TickerTitle").setContentTitle("Content Title").
         setContentText("Hello how are you").setSmallIcon(R.mipmap.ic_launcher)
         .setContentIntent(pendingIntent);
 NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notification);//error

Upvotes: 0

Views: 39

Answers (1)

tostao
tostao

Reputation: 3068

Here you can find all information about Android Notification - https://developer.android.com/develop/ui/views/notifications#java

And here howto build sample notification https://developer.android.com/develop/ui/views/notifications/build-notification#java

and the sample looks like:

// Create an explicit intent for an Activity in your app.
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    // Set the intent that fires when the user taps the notification.
    .setContentIntent(pendingIntent)
    .setAutoCancel(true);

Upvotes: 1

Related Questions