繁体   English   中英

如何以编程方式关闭通知

[英]How to turn notification off programatically

我有一个 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);
            }
        }

通知接收器.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();
        }
    }

通知接收器.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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM