如何以编程方式关闭通知
How to turn notification off programatically
我有一个 4 单选按钮通知类型 0,1,2,3
如果用户 select 通知类型 0 ,则用户不应收到通知。
如何以编程方式进行。
(notificationType != 0) {
if (notificationType == 1) {
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
if (notificationType == 2) {
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.azan);
}
if (notificationType == 3) {
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.mohsin_mava);
}
}
NotificaitionReciever.java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannelFajr = notificationManager.getNotificationChannel(FAJR_CHANNEL_ID);
if (mChannelFajr == null) {
mChannelFajr = new NotificationChannel(FAJR_CHANNEL_ID, fajrChannelName, importance);
mChannelFajr.setDescription(notificationText);
mChannelFajr.enableVibration(true);
mChannelFajr.enableLights(true);
mChannelFajr.setLockscreenVisibility(VISIBILITY_PUBLIC);
mChannelFajr.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(mChannelFajr);
}
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ") // required
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name));
} else {
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ")
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name))
.setPriority(Notification.PRIORITY_HIGH);
}
notification = builder.build();
notificationManager.notify(0, notification);
这里我做了一个通知渠道
如何以编程方式将其关闭?
public void cancelNotification(int requestCode) {
try {
Intent notificationIntent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
NotificaitionReciever.java
if(notificationtype ==0)
{
cancelnotification(requestcode)
}
else{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannelFajr = notificationManager.getNotificationChannel(FAJR_CHANNEL_ID);
if (mChannelFajr == null) {
mChannelFajr = new NotificationChannel(FAJR_CHANNEL_ID, fajrChannelName, importance);
mChannelFajr.setDescription(notificationText);
mChannelFajr.enableVibration(true);
mChannelFajr.enableLights(true);
mChannelFajr.setLockscreenVisibility(VISIBILITY_PUBLIC);
mChannelFajr.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(mChannelFajr);
}
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ") // required
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name));
} else {
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ")
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name))
.setPriority(Notification.PRIORITY_HIGH);
}
notification = builder.build();
notificationManager.notify(0, notification);
}
我有一个 4 单选按钮通知类型 0,1,2,3 如果用户 select 通知类型 0 ,则用户不应收到通知。 如何以编程方式进行。
(notificationType != 0) {
if (notificationType == 1) {
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
if (notificationType == 2) {
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.azan);
}
if (notificationType == 3) {
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.mohsin_mava);
}
}
NotificaitionReciever.java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannelFajr = notificationManager.getNotificationChannel(FAJR_CHANNEL_ID);
if (mChannelFajr == null) {
mChannelFajr = new NotificationChannel(FAJR_CHANNEL_ID, fajrChannelName, importance);
mChannelFajr.setDescription(notificationText);
mChannelFajr.enableVibration(true);
mChannelFajr.enableLights(true);
mChannelFajr.setLockscreenVisibility(VISIBILITY_PUBLIC);
mChannelFajr.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(mChannelFajr);
}
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ") // required
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name));
} else {
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ")
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name))
.setPriority(Notification.PRIORITY_HIGH);
}
notification = builder.build();
notificationManager.notify(0, notification);
这里我做了一个通知渠道 如何以编程方式将其关闭?
public void cancelNotification(int requestCode) {
try {
Intent notificationIntent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
NotificaitionReciever.java
if(notificationtype ==0)
{
cancelnotification(requestcode)
}
else{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannelFajr = notificationManager.getNotificationChannel(FAJR_CHANNEL_ID);
if (mChannelFajr == null) {
mChannelFajr = new NotificationChannel(FAJR_CHANNEL_ID, fajrChannelName, importance);
mChannelFajr.setDescription(notificationText);
mChannelFajr.enableVibration(true);
mChannelFajr.enableLights(true);
mChannelFajr.setLockscreenVisibility(VISIBILITY_PUBLIC);
mChannelFajr.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(mChannelFajr);
}
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ") // required
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name));
} else {
builder = new NotificationCompat.Builder(context, FAJR_CHANNEL_ID);
builder.setContentTitle("FAJR NAMAZ")
.setSmallIcon(R.mipmap.ic_darshika_logo) // required
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_darshika_logo))
.setContentText(notificationText) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSound(soundUri)
.setLights(Color.MAGENTA, 500, 500)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setTicker(context.getString(R.string.app_name))
.setPriority(Notification.PRIORITY_HIGH);
}
notification = builder.build();
notificationManager.notify(0, notification);
}