Notification service put extra on notification click, how?

10,724

Solution 1

I'm guessing that your problem is here:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
                           0, intent, 0);

You are passing intent to getActivity() and expecting that you will get back a PendingIntent that matches your Intent and includes your extras. Unfortunately, if there is already a PendingIntent floating around in the system that matches your Intent (without taking into consideration your Intent extras) then getActivity() will return you that PendingIntent instead.

To see if this is the problem, try this:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
                           0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

This says that if there is already a PendingIntent that matches your Intent somewhere in the system that it should replace the extras with the ones in your intent parameter.

Solution 2

I had the same problem. You used getIntent().getExtras() but you should have used getIntent().getStringExtra(id). It's mentioned here too: Android: intent.putExtra in a Service Class to get notifications

Also it's considered not to be a good practice to use static variables as you've mentioned in your comment, I'd suggest to avoid it.

Share:
10,724
Rotary Heart
Author by

Rotary Heart

Updated on June 08, 2022

Comments

  • Rotary Heart
    Rotary Heart almost 2 years

    Well I have a service that runs with a notification, I want to put an extra with the new intent opened when the user clicks the notification, so when MainActivity opens I can get the extra. I have tried with different ways, but I just don't find how to get it to work.

    Here's what I have tried:

    This is MyService

    Intent intent = new Intent(MyService.this, MainActivity.class);
                        intent.putExtra("EXTRA", StringTEST);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        PendingIntent contentIntent = PendingIntent
                            .getActivity(getApplicationContext(),
                                         0, intent, 0);
    

    this opens the activity fine, but I can't get the extra?? This is how I have tried to get it:

    MainActivity

    @Override
    public void onNewIntent(Intent intent){
        Bundle extras = intent.getExtras();
        if(extras != null){
            String name = extras.getString("EXTRA");
            System.out.println(name);
        }
    }
    
    @Override
    protected void onResume() {
        Bundle extras = getIntent().getExtras();
        System.out.println(extras+"");
        if (extras != null) {
            try {
                onNewIntent(getIntent());
                Intent i = getIntent();
                String name = i.getStringExtra("EXTRA");
                System.out.println(name);
                currentDir = new File(name);
    
                changeFiles();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.onResume();
    }
    

    Those are the two ways I have tried, but I still get null... What am I doing wrong?

    Thanks

  • Rotary Heart
    Rotary Heart over 11 years
    Tried this and I didn't get it either, but I managed to got it to work making it a static String I know is not the best way, but...
  • David Wasser
    David Wasser over 11 years
    Glad you got it working. I still don't understand what the problem is. Maybe you'll figure it out. If you do, please answer your own question so that others can benefit from it.
  • DaMachk
    DaMachk about 9 years
    This helped in my case. Thanks! :)
  • phil
    phil almost 9 years
    Thank you very much! Now it is possible to get different extras into multiple notification
  • David Wasser
    David Wasser almost 9 years
    @flipperweid There are many questions on stackoverflow about this. Just look. Basically you need to ensure your PendingIntent is unique. You can do that by using a unique requestCode in each call to PendingIntent.getActivity() or you can set a unique ACTION on each PendingIntent.
  • phil
    phil almost 9 years
    Hi, yes I use that. My comment was meant only as a "thank you!:-)" , cause before I use the 'FLAG_UPDATE_CURRENT every clicked Notification had the same extras altough I had set different extras in different notifications. Now it work's:-))
  • Siddharth Sachdeva
    Siddharth Sachdeva over 6 years
    To my surprise, it actually worked, however, I was casting it to string in the other case.