【问题标题】:Delete notification after clicking on 'Delete'点击“删除”后删除通知
【发布时间】:2021-08-02 18:19:47
【问题描述】:

我的应用中有一条通知,其中包含以下代码:

@SuppressLint("RestrictedApi")
    void ShowNotification(String title, String text, int color) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelId = getString(R.string.default_notification_channel_id);
            String description = getString(R.string.channel_description);
            NotificationChannel channel = new NotificationChannel(channelId, "MyNew notification", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
        Intent deleteIntent = new Intent(this, MainActivity.class);
        deleteIntent.setAction(Intent.ACTION_DELETE);
        deleteIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
        PendingIntent deletePendingIntent = PendingIntent.getService(this, 0, deleteIntent, 0);

        // Create pending intent, mention the Activity which needs to be
        //triggered when user clicks on notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mNotify = new NotificationCompat.Builder(this, channelId);
        mNotify.setLights(color, 100 , 200);
        mNotify.setSmallIcon(R.drawable.ic_action_name);
        mNotify.setLargeIcon(BitmapFactory. decodeResource (getResources() , R.drawable.ic_action_name)) ;
        mNotify.setContentTitle(title);
        mNotify.setOngoing(true);

        mNotify.setShowWhen(true);
        mNotify.setContentText(text);
        mNotify.setStyle(new NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line..."));
        mNotify.setDefaults(Notification.DEFAULT_SOUND);

        mNotify.addAction(R.mipmap.shield3_foreground, "Delete", deletePendingIntent);
        mNotify.setContentIntent(contentIntent);//Activity which needs to be triggered when user clicks on notification
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        int mId = 1001;
        try {
            mNotificationManager.notify(mId, mNotify.build());

        }
        catch (Exception e) { e.printStackTrace(); }
    }

我的通知工作正常,但问题是,当我在通知中单击“删除”时,它不会删除通知。

我明白,我必须在我的代码中使用 cancel(),但不知道如何在此处使用它以及在何处使用此方法。我应该怎么做才能在我的通知中使用“删除”?谢谢。

【问题讨论】:

    标签: java android firebase notifications


    【解决方案1】:

    使用BroadCastReceiver

    1. deleteIntentBroadCastReceiver.class 设置
    2. deletePendingIntentPendingIntent.getBroadCast() 设置
    3. cancel(id)cancelAll()BroadCastReceiver

    更多细节: android codelabs帮你。

    更新

    在您的代码中,您的deleteIntent 是为MainActivitydeletePendingIntent 是来自Service,它不匹配。

    如果您不想使用ServiceBroadcastReceiver 而只想使用MainActivity,请更改如下代码

    Intent deleteIntent = new Intent(context, MainActivity.class);
    deleteIntent.setAction(Intent.ACTION_DELETE);
    //getService() -> getActivity()
    PendingIntent deletePendingIntent = PendingIntent.getActivity(context, 0, deleteIntent, 0);
    

    你需要处理那个动作

    //onCreate() in MainActivity
    String action = getIntent().getAction();
    if (action != null && action.equals(Intent.ACTION_DELETE)) {
        notificationUtil.deleteNotification(this);
        //if you don't want open app
        finish();
    }
    
    //...
    
    //in NotificationUtil
    void deleteNotification(Context context) {
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(mId);
    }
    

    这是个坏主意

    【讨论】:

    • 感谢您的回复,但我不需要从某处删除通知。我只想使用“删除”(不是滑动)将其从屏幕上删除。感谢 Kotlin 示例,但它对我没有帮助。可以分享一下Java代码吗?欣赏。
    • 我更新答案。并阅读BroadcastReceiver 文档。它适合您的情况。(如果您不想显示Activity
    • 非常感谢!有用。我在 notificationUtil.deleteNotification(this) 中删除了 notificationUtil,现在它完美了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2010-09-27
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多