如何使用Notification.deleteIntent

发布于 2024-12-05 15:03:40 字数 802 浏览 0 评论 0原文

我正在尝试检测我的通知何时被清除。我的问题直接引用了这个答案,它概述了我的想法去做。这就是我实现操作的方式:

// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)

这是 CleanUpIntent 类:

class CleanUpIntent extends IntentService {
    public CleanUpIntent() {
        super("CleanUpIntent");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // clean up code
    }
}

之后,我只是像平常一样启动通知,但当我去测试它时(按“清除所有通知”)什么也没有发生。我插入了一行代码,当 IntentService 启动时,该代码会向 LogCat 打印一些内容,但没有运行任何内容。这是我应该使用Notification.deleteIntent的方式吗?

I'm trying to detect when my notification gets cleared. My question directly refers to this answer which outlines what I'm suppose to do. This is how I'm implementing the actions:

// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)

This is the CleanUpIntent class:

class CleanUpIntent extends IntentService {
    public CleanUpIntent() {
        super("CleanUpIntent");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // clean up code
    }
}

Afterwards, I simply launch the notification like I normally would but when I go to test it out (pressing "Clear All Notifications") nothing happens. I inserted a line of code that out print something to LogCat when the IntentService gets started, but nothing ever ran. Is this how I'm suppose to use Notification.deleteIntent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

演出会有结束 2024-12-12 15:03:40

每当用户清除通知时都会调用的示例代码,希望对您有所帮助。

 ....
 notificationBuilder.setDeleteIntent(getDeleteIntent());
 ....
  

protected PendingIntent getDeleteIntent()
{
    Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

NotificationBroadcastReceiver.java

public class NotificationBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if(action != null && action.equals("notification_cancelled"))
        {
            // your code
        }
    }
}

AndroidManifyingst.xml

 <receiver
     android:name=".NotificationBroadcastReceiver"
     android:exported="false">
     <intent-filter>
         <action android:name="notification_cancelled"/>
     </intent-filter>
 </receiver>

sample code which will be called whenever user clears the notification, hope it will help you .

 ....
 notificationBuilder.setDeleteIntent(getDeleteIntent());
 ....
  

protected PendingIntent getDeleteIntent()
{
    Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class);
    intent.setAction("notification_cancelled");
    return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

NotificationBroadcastReceiver.java

public class NotificationBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if(action != null && action.equals("notification_cancelled"))
        {
            // your code
        }
    }
}

AndroidManifiest.xml

 <receiver
     android:name=".NotificationBroadcastReceiver"
     android:exported="false">
     <intent-filter>
         <action android:name="notification_cancelled"/>
     </intent-filter>
 </receiver>
等往事风中吹 2024-12-12 15:03:40

您需要做的是注册一个 BroadcastReceiver (可能在 AndroidManifest.xml 中,或者在 Service 中使用 registerReceiver),然后设置 deleteIntent 是一个将被该接收者捕获的Intent

What you need to do is register a BroadcastReceiver (probably in your AndroidManifest.xml or alternatively using registerReceiver in a Service) and then set deleteIntent to be an Intent that will be caught by that receiver.

拿命拼未来 2024-12-12 15:03:40

您应该使用 getBroadcast 方法而不是 getService ,并且应该为特定操作注册接收器。

You should use getBroadcast methode instead getService and should register receiver for specific Action.

瑾夏年华 2024-12-12 15:03:40

不需要显式接收器。当按下clear按钮时,deleteIntent将被自动调用。

An explicit receiver is not required. deleteIntent will be called automatically while pressing the clear button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文