抓住刷卡来解雇事件

时间:2022-04-06 23:59:53

I'm using an android notification to alert the user once a service is finished (success or failure), and I want to delete local files once the process is done.

我正在使用android通知在服务完成(成功或失败)后提醒用户,并且我想在完成该过程后删除本地文件。

My problem is that in the event of failure - I want to let the user a "retry" option. and if he chooses not to retry and to dismiss the notification I want to delete local files saved for the process purposes (images...).

我的问题是,如果发生故障 - 我想让用户“重试”选项。如果他选择不重试并解除通知,我想删除为处理目的而保存的本地文件(图像......)。

Is there a way to catch the notification's swipe-to-dismiss event?

有没有办法捕获通知的刷卡到解雇事件?

3 个解决方案

#1


113  

DeleteIntent: DeleteIntent is a PendingIntent object that can be associated with a notification and gets fired when the notification gets deleted, ether by :

DeleteIntent:DeleteIntent是一个PendingIntent对象,可以与通知关联,并在通知被删除时被触发,ether by:

  • User specific action
  • 用户特定的操作
  • User Delete all the notifications.
  • 用户删除所有通知。

You can set the Pending Intent to a broadcast Receiver and then perform any action you want.

您可以将Pending Intent设置为广播Receiver,然后执行所需的任何操作。

  Intent intent = new Intent(this, MyBroadcastReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
  Builder builder = new Notification.Builder(this):
 ..... code for your notification
  builder.setDeleteIntent(pendingIntent);

MyBroadcastReceiver

MyBroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
             .... code to handle cancel
         }

  }

#2


69  

A fully flushed out answer (with thanks to Mr. Me for the answer):

一个完全刷新的答案(感谢我先生的答案):

1) Create a receiver to handle the swipe-to-dismiss event:

1)创建一个接收器来处理滑动到解除事件:

public class NotificationDismissedReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
      /* Your code to handle the event here */
  }
}

2) Add an entry to your manifest:

2)在清单中添加一个条目:

<receiver
    android:name="com.my.app.receiver.NotificationDismissedReceiver"
    android:exported="false" >
</receiver>

3) Create the pending intent using a unique id for the pending intent (the notification id is used here) as without this the same extras will be reused for each dismissal event:

3)使用未决意图的唯一ID(此处使用通知ID)创建待定意图,因为没有这个,将为每个解雇事件重复使用相同的额外内容:

private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("com.my.app.notificationId", notificationId);

    PendingIntent pendingIntent =
           PendingIntent.getBroadcast(context.getApplicationContext(), 
                                      notificationId, intent, 0);
    return pendingIntent;
}

4) Build your notification:

4)建立你的通知:

Notification notification = new NotificationCompat.Builder(context)
              .setContentTitle("My App")
              .setContentText("hello world")
              .setWhen(notificationTime)
              .setDeleteIntent(createOnDismissedIntent(context, notificationId))
              .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);

#3


0  

Another Idea:

另一个想法:

if you create a notification normally you also need the actions one, two or 3 of them. I've created a "NotifyManager" it creates all notifications i need and also receive all Intent calls. So i can manage all the actions AND also the catch the dismiss event at ONE place.

如果您通常创建通知,则还需要其中一个,两个或三个操作。我创建了一个“NotifyManager”,它创建了我需要的所有通知,并且还接收了所有Intent调用。所以我可以管理所有的行动,也可以在一个地方捕捉解雇事件。

public class NotifyPerformService extends IntentService {

@Inject NotificationManager notificationManager;

public NotifyPerformService() {
    super("NotifyService");
    ...//some Dagger stuff
}

@Override
public void onHandleIntent(Intent intent) {
    notificationManager.performNotifyCall(intent);
}

to create the deleteIntent use this (in the NotificationManager):

创建deleteIntent使用它(在NotificationManager中):

private PendingIntent createOnDismissedIntent(Context context) {
    Intent          intent          = new Intent(context, NotifyPerformMailService.class).setAction("ACTION_NOTIFY_DELETED");
    PendingIntent   pendingIntent   = PendingIntent.getService(context, SOME_NOTIFY_DELETED_ID, intent, 0);

    return pendingIntent;
}

and THAT i use to set the delete Intent like this (in the NotificationManager):

我用这个来设置删除意图(在NotificationManager中):

private NotificationCompat.Builder setNotificationStandardValues(Context context, long when){
    String                          subText = "some string";
    NotificationCompat.Builder      builder = new NotificationCompat.Builder(context.getApplicationContext());


    builder
            .setLights(ContextUtils.getResourceColor(R.color.primary) , 1800, 3500) //Set the argb value that you would like the LED on the device to blink, as well as the rate
            .setAutoCancel(true)                                                    //Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
            .setWhen(when)                                                          //Set the time that the event occurred. Notifications in the panel are sorted by this time.
            .setVibrate(new long[]{1000, 1000})                                     //Set the vibration pattern to use.

            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setSmallIcon(R.drawable.ic_white_24dp)
            .setGroup(NOTIFY_GROUP)
            .setContentInfo(subText)
            .setDeleteIntent(createOnDismissedIntent(context))
    ;

    return builder;
}

and finally in the same NotificationManager is the perform function:

最后在同一个NotificationManager中是perform函数:

public void performNotifyCall(Intent intent) {
    String  action  = intent.getAction();
    boolean success = false;

    if(action.equals(ACTION_DELETE)) {
        success = delete(...);
    }

    if(action.equals(ACTION_SHOW)) {
        success = showDetails(...);
    }

    if(action.equals("ACTION_NOTIFY_DELETED")) {
        success = true;
    }


    if(success == false){
        return;
    }

    //some cleaning stuff
}

#1


113  

DeleteIntent: DeleteIntent is a PendingIntent object that can be associated with a notification and gets fired when the notification gets deleted, ether by :

DeleteIntent:DeleteIntent是一个PendingIntent对象,可以与通知关联,并在通知被删除时被触发,ether by:

  • User specific action
  • 用户特定的操作
  • User Delete all the notifications.
  • 用户删除所有通知。

You can set the Pending Intent to a broadcast Receiver and then perform any action you want.

您可以将Pending Intent设置为广播Receiver,然后执行所需的任何操作。

  Intent intent = new Intent(this, MyBroadcastReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
  Builder builder = new Notification.Builder(this):
 ..... code for your notification
  builder.setDeleteIntent(pendingIntent);

MyBroadcastReceiver

MyBroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
             .... code to handle cancel
         }

  }

#2


69  

A fully flushed out answer (with thanks to Mr. Me for the answer):

一个完全刷新的答案(感谢我先生的答案):

1) Create a receiver to handle the swipe-to-dismiss event:

1)创建一个接收器来处理滑动到解除事件:

public class NotificationDismissedReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
      /* Your code to handle the event here */
  }
}

2) Add an entry to your manifest:

2)在清单中添加一个条目:

<receiver
    android:name="com.my.app.receiver.NotificationDismissedReceiver"
    android:exported="false" >
</receiver>

3) Create the pending intent using a unique id for the pending intent (the notification id is used here) as without this the same extras will be reused for each dismissal event:

3)使用未决意图的唯一ID(此处使用通知ID)创建待定意图,因为没有这个,将为每个解雇事件重复使用相同的额外内容:

private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("com.my.app.notificationId", notificationId);

    PendingIntent pendingIntent =
           PendingIntent.getBroadcast(context.getApplicationContext(), 
                                      notificationId, intent, 0);
    return pendingIntent;
}

4) Build your notification:

4)建立你的通知:

Notification notification = new NotificationCompat.Builder(context)
              .setContentTitle("My App")
              .setContentText("hello world")
              .setWhen(notificationTime)
              .setDeleteIntent(createOnDismissedIntent(context, notificationId))
              .build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);

#3


0  

Another Idea:

另一个想法:

if you create a notification normally you also need the actions one, two or 3 of them. I've created a "NotifyManager" it creates all notifications i need and also receive all Intent calls. So i can manage all the actions AND also the catch the dismiss event at ONE place.

如果您通常创建通知,则还需要其中一个,两个或三个操作。我创建了一个“NotifyManager”,它创建了我需要的所有通知,并且还接收了所有Intent调用。所以我可以管理所有的行动,也可以在一个地方捕捉解雇事件。

public class NotifyPerformService extends IntentService {

@Inject NotificationManager notificationManager;

public NotifyPerformService() {
    super("NotifyService");
    ...//some Dagger stuff
}

@Override
public void onHandleIntent(Intent intent) {
    notificationManager.performNotifyCall(intent);
}

to create the deleteIntent use this (in the NotificationManager):

创建deleteIntent使用它(在NotificationManager中):

private PendingIntent createOnDismissedIntent(Context context) {
    Intent          intent          = new Intent(context, NotifyPerformMailService.class).setAction("ACTION_NOTIFY_DELETED");
    PendingIntent   pendingIntent   = PendingIntent.getService(context, SOME_NOTIFY_DELETED_ID, intent, 0);

    return pendingIntent;
}

and THAT i use to set the delete Intent like this (in the NotificationManager):

我用这个来设置删除意图(在NotificationManager中):

private NotificationCompat.Builder setNotificationStandardValues(Context context, long when){
    String                          subText = "some string";
    NotificationCompat.Builder      builder = new NotificationCompat.Builder(context.getApplicationContext());


    builder
            .setLights(ContextUtils.getResourceColor(R.color.primary) , 1800, 3500) //Set the argb value that you would like the LED on the device to blink, as well as the rate
            .setAutoCancel(true)                                                    //Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
            .setWhen(when)                                                          //Set the time that the event occurred. Notifications in the panel are sorted by this time.
            .setVibrate(new long[]{1000, 1000})                                     //Set the vibration pattern to use.

            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setSmallIcon(R.drawable.ic_white_24dp)
            .setGroup(NOTIFY_GROUP)
            .setContentInfo(subText)
            .setDeleteIntent(createOnDismissedIntent(context))
    ;

    return builder;
}

and finally in the same NotificationManager is the perform function:

最后在同一个NotificationManager中是perform函数:

public void performNotifyCall(Intent intent) {
    String  action  = intent.getAction();
    boolean success = false;

    if(action.equals(ACTION_DELETE)) {
        success = delete(...);
    }

    if(action.equals(ACTION_SHOW)) {
        success = showDetails(...);
    }

    if(action.equals("ACTION_NOTIFY_DELETED")) {
        success = true;
    }


    if(success == false){
        return;
    }

    //some cleaning stuff
}