Android Notification Action is not fired (PendingIntent)

前端 未结 9 1497
盖世英雄少女心
盖世英雄少女心 2020-12-30 02:39

I am trying to add an Notification action item in my app which is a music player. When a stream is started a notification should be triggered and an stop button for the stre

相关标签:
9条回答
  • 2020-12-30 02:56

    This is very late answer but it may help someone:

    You should choose the right kind of Pending intent based on the intent you want to run. Here are some Examples:

    For Activity use below:

    Intent i = new Intent(this, YourActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
    

    For Service use below:

    Intent i = new Intent(this, YourService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    

    For Broadcast Receiver use below:

    Intent i = new Intent(this, YourReciver.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    

    You may need to change the request code and Flags if required

    0 讨论(0)
  • 2020-12-30 02:59

    You do not receive the broadcast when it is in the background because you are unregistering in onPause. Move the unregisterReceiver code to onDestroy function. This will be called only when the activity is destroyed. Or you can unregister once the expected event has occurred.

    0 讨论(0)
  • 2020-12-30 03:00

    I find solution in this thread on google code https://code.google.com/p/android/issues/detail?id=61850

    To fix it you must add PendingIntent.FLAG_CANCEL_CURRENT flag to your PendingIntent.

    PendingIntent stopPendingIntent2 = PendingIntent.getBroadcast(this, 0,
                stopIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
    
    0 讨论(0)
  • 2020-12-30 03:05

    I ran into this problem today. In my case it was using cached intent extras from a previous instance of the intent as all the parameters for the pendingIntent constructors was same. I found two solutions for this...

    1. Using FLAG_CANCEL_CURRENT as mentioned by Nik.
    2. Passing an unique requestCode to the pendingIntent as follows

      PendingIntent pi = PendingIntent.getService(this, UNIQUE_ID, pi, 0);
      

    In my case, the second method solved the problem as I need to keep the previous notifications alive. May be this will help someone with similar issue.

    0 讨论(0)
  • 2020-12-30 03:06

    There are multiple questions here:

    Part 1: Why is your intent not receiving the "STOP" extra? Though it is not seen in the code you have provided, I wanted to confirm if you are using the flag FLAG_ACTIVITY_SINGLE_TOP for the notification intent ? If so, the intent you receive in your activity would be the intent that started the activity and hence the "STOP" extra will not be available. You will need to extend the onNewIntent() is this case (where the new intent is sent). More info here.
    If you have not used FLAG_ACTIVITY_SINGLE_TOP, then it means that a new activity is created when notification is tapped, in which case the Intent must have the "STOP" parameter. If you can provide me all relevant code, I can help you better.

    Part 2: Using Broadcast Receiver This is not straight forward, as you already discovered. You would need to unregister in onDestroy and if your activity is closed by the user, the onDestroy may be called and your broadcast receiver may not active at the time the notification is tapped by the user. If you dont unregister at all, it may seem to be working, but this is a memory leak, GC may clean up anytime which could lead to a crash in your program, ie., you MUST unregister. If you need to go with broadcast receiver approach, you need to have a service to do this and service comes with its own pitfalls -> restart by system, battery drain etc. I would strongly recommend you go with your first approach.

    0 讨论(0)
  • 2020-12-30 03:08

    I had a very similar issue but a very different solution. Pending intent is also not fired if you have declared <service android:enabled="false"></service> in your manifest.xml file.

    Replace from android:enabled="false" to android:enabled="true"

    This might not be a direct issue of the problem. But if you create the service in android studio using default template it automatically adds these properties to the service.

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