java—如何通过编程关闭通知

i5desfxk  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(613)

我有一个4单选按钮通知类型0,1,2,3如果用户选择通知类型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);
            }
        }

NotificationReceiver.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);

在这里,我已经为通知如何关闭它编程通道?

k97glaaz

k97glaaz1#

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();
        }
    }

NotificationReceiver.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);
}

相关问题