How to use Notification.deleteIntent

后端 未结 4 2120

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

相关标签:
4条回答
  • 2020-12-14 10:01

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

    0 讨论(0)
  • 2020-12-14 10:07

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

    0 讨论(0)
  • 2020-12-14 10:18

    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

    @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if(action.equals("notification_cancelled"))
            {
                // your code
            }
        }
    

    AndroidManifiest.xml

     <receiver android:name=".NotificationBroadcastReceiver">
                    <intent-filter>
                        <action android:name="notification_cancelled"/>
                    </intent-filter>
                </receiver>
    
    0 讨论(0)
  • 2020-12-14 10:25

    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.

    0 讨论(0)
提交回复
热议问题