Is it possible to check if a notification is visible or canceled?

前端 未结 6 1662
别那么骄傲
别那么骄傲 2020-12-08 06:31

I would like to update notification data, but the only way I found is to launch a new one with the same Id.

The problem is that I don\'t want to raise a new one if t

6条回答
  •  青春惊慌失措
    2020-12-08 06:57

    An alternative to the deleteIntent is the following which has proved beneficial in my own app:

    Basically, you create an intent with your notification that starts an IntentService (or any other service) and in onHandleIntent you can set a flag indicating whether the notification is active.
    You can set this intent to be fired when the user taps the notification (contentIntent) and/or when the user clears it from the list (deleteIntent).

    To illustrate it, here is what I do in my own app. When building the notification I set

    Intent intent = new Intent(this, CleanupIntentService.class);
    Notification n = NotificationCompat.Builder(context).setContentIntent(
            PendingIntent.getActivity(this, 0, intent, 0)).build();
    

    When the notification is tapped, my CleanupIntentService is launched, setting a flag (in the service that created the notification):

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onCreate(); // If removed, onHandleIntent is not called
        return super.onStartCommand(intent, flags, startId);
    }
    
    
    @Override
    protected void onHandleIntent(Intent intent) {
        OtherService.setNotificationFlag(false);
    }
    

提交回复
热议问题