The notification service adds more information about the notification, how?

Well I have a service that starts with a notification, I want to add an additional one with a new intent open when the user clicks the notification, so when it MainActivity

opens I can get additional ones. I've tried in different ways, but I just don't know how to get it to work.

Here's what I've 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 an activity but i cant get additional ??? This is how I 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();
}

      

These are the two paths I tried, but I still get null ... What am I doing wrong?

thank

+3


source to share


2 answers


I guess your problem is here:

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

      

You pass intent

in getActivity()

and expect to return to PendingIntent

that matches yours intent

and includes your additional functionality. Unfortunately, if the system already has PendingIntent

one that matches yours intent

( without taking into account your additional functions intent

), then it getActivity()

will return you that PendingIntent

instead.



To verify this is the problem, try the following:

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

      

This suggests that if there already exists PendingIntent

one that matches yours intent

somewhere in the system, that it should replace additional parameters in the parameter intent

.

+20


source


I had the same problem. You used getIntent().getExtras()

, but you had to use getIntent().getStringExtra(id)

. It is also mentioned here: Android: intent.putExtra in the service class for receiving notifications



It is also considered to be discouraged from using static variables as you mentioned in your comment, I would suggest avoiding this.

+2


source







All Articles