Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification service put extra on notification click, how?

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

like image 706
Rotary Heart Avatar asked Jan 20 '13 23:01

Rotary Heart


People also ask

How do I increase notification on Android?

To expand a notification, tap the Down arrow . Then, to act directly from a notification, tap an action, like Reply or Archive. Some apps show a dot when you get a notification. Touch and hold the app with the dot to see the oldest notification.

How do I manage multiple notifications on Android?

Set the unique id to let Notification Manager knows this is a another notification instead of same notification. If you use the same unique Id for each notification, the Notification Manager will assume that is same notification and would replace the previous notification.

How do I get notification bar notifications?

The user can reveal the Notifications window by pulling down the status bar (or selecting Notifications from the Home options menu).


2 Answers

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.

like image 128
David Wasser Avatar answered Oct 04 '22 15:10

David Wasser


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.

like image 41
abbath Avatar answered Oct 04 '22 17:10

abbath