Android: Using AUTO-CANCEL on a notification when your app is running in the background

后端 未结 3 777
猫巷女王i
猫巷女王i 2020-12-14 02:25

I have looked at all the other AUTO-CANCEL-not-working questions here, and they all seem to involve mistakes that I am not making. I have tried both

builder         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 03:07

    So apparently you do need a pending intent.

    At Android - notification manager, having a notification without an intent, I found a solution that grabs the current active application as your pending intent (so that you don't have to start your own activity in order to dismiss the notification).

    I just added the following two lines of code (right after setting the auto-cancel):

    PendingIntent notifyPIntent = 
        PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);     
    builder.setContentIntent(notifyPIntent);
    

    It worked great. I would say that if you don't want your activity to restart as a result of the user clicking your notification, then this is your best option.

提交回复
热议问题