How to Create Custom Notifications with Listeners
MoreBlock Code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel name 1";
String description = "Notification channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("id 1", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
OnButtonClicked:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.cview);
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Intent intent1 = new Intent(MainActivity.this, ThreeActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent1 = PendingIntent.getActivity(MainActivity.this, 0, intent1, 0);
Intent intent2 = new Intent(MainActivity.this, FourActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent2 = PendingIntent.getActivity(MainActivity.this, 0, intent2, 0);
androidx.core.app.NotificationCompat.Builder builder = new androidx.core.app.NotificationCompat.Builder(MainActivity.this, "id 1")
.setSmallIcon(R.drawable.img)
.setContent(contentView)
.setOngoing(false)
.setPriority(androidx.core.app.NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
contentView.setOnClickPendingIntent(R.id.one,pendingIntent);
contentView.setOnClickPendingIntent(R.id.two,pendingIntent1);
contentView.setOnClickPendingIntent(R.id.three,pendingIntent2);
androidx.core.app.NotificationManagerCompat notificationManager = androidx.core.app.NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(1, builder.build());
Comments
Post a Comment