0

Recently, I have been meticulously looking over these two questions and solutions to the issue of trying to have multiple notifications that each have their own activities that grab String extras from their own corresponding notification.

How to pass text from notification to another activity? How to send parameters from a notification-click to an activity?

For the most part the current solution works except for one key issue: When the application is not open, and I receive two different notifications. I click to open the notification detail activity and the text in the activity updates just fine. However, while keeping the same activity up, I click on the second notification and nothing happens.

When the application is open, this solution works fine. It opens two separate intents with two different notification text views. If the application is closed, and I exit out of the first notifications view details activity and then open up the second, it also accomplishes the solution fine.

Any help would be greatly appreciated!

Here is my code:

MainActivity class

private NotificationManager notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

public void setAlarm1(View view){
    Toast.makeText(getApplicationContext(),"Setting Alarm 5 seconds",Toast.LENGTH_SHORT).show();
    //this is 5 seconds
    long alertTime = new GregorianCalendar().getTimeInMillis();
    alertTime += 5*1000;


    Intent alertIntent = new Intent(this,AlertReceiver.class);
    alertIntent.putExtra("msg","This is msg for 5 seconds");
    alertIntent.putExtra("msgText","This is msgText for 5 seconds");
    alertIntent.putExtra("msgAlert","This is msgAlert for 5 seconds");
    alertIntent.putExtra("notifyID",1);

    AlarmManager alarmManager = (AlarmManager)
            getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP,alertTime,
            PendingIntent.getBroadcast(this,1,alertIntent,
                    0));

}

public void setAlarm2(View view){
    Toast.makeText(getApplicationContext(),"Setting Alarm 10 seconds",Toast.LENGTH_SHORT).show();
    //this is 5 seconds
    long alertTime = new GregorianCalendar().getTimeInMillis();
    alertTime += 10*1000;


    Intent alertIntent = new Intent(this,AlertReceiver.class);
    alertIntent.putExtra("msg","This is msg for 10 seconds");
    alertIntent.putExtra("msgText","This is msgText for 10 seconds");
    alertIntent.putExtra("msgAlert","This is msgAlert for 10 seconds");
    alertIntent.putExtra("notifyID",2);

    AlarmManager alarmManager = (AlarmManager)
            getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP,alertTime,
            PendingIntent.getBroadcast(this,2,alertIntent,
                    0));

}

AlertReceiver class

public class AlertReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    createNotification(context, intent.getStringExtra("msg"), intent.getStringExtra("msgText"), intent.getStringExtra("msgAlert"), intent.getIntExtra("notifyID",999));

}

public void createNotification(Context context, String msg, String msgText, String msgAlert, int notifyID){
    Intent intent = new Intent(context, MoreInfoNotification.class);
    intent.putExtra("msg",msg);
    intent.putExtra("msgText",msgText);
    intent.putExtra("msgAlert",msgAlert);
    // had this below line of code but made problems worse
    // intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent notificIntent = PendingIntent.getActivity(context, notifyID,
            intent,PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
    style.setBigContentTitle(msg);
    style.bigText(msgText);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(msg);
    builder.setContentText(msgText);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setTicker(msgAlert);


    builder.setStyle(style);

    builder.setContentIntent(notificIntent);


    builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    builder.setAutoCancel(true);

    NotificationManager mNotificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notifyID,builder.build());

}
}

MoreInfoNotification class

public class MoreInfoNotification extends AppCompatActivity {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    onNewIntent(getIntent());

}

@Override
public void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    Bundle extras = getIntent().getExtras();
    if(extras != null){

        setContentView(R.layout.activity_more_info_notification);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mTextView = (TextView)findViewById(R.id.textView);
        String totalString = "";

        if(extras.containsKey("msg")){
            totalString += extras.getString("msg");
        }
        if(extras.containsKey("msgText")){
            totalString += ", " + extras.getString("msgText");
        }
        if(extras.containsKey("msgAlert")){
            totalString += ", " + extras.getString("msgAlert");
        }

        mTextView.setText(totalString);
    }
}
}
Community
  • 1
  • 1

1 Answers1

0

So I recently found the answer to this problem. The Intents were seen as the same instead of separate, which is what I was looking for. In order to get the Intents away from being viewed as duplicates I wrote this line:

    intent.setAction("com.wordpress.zackleaman.materialtablayout.intent.action.ACTION_NAME" + notifyID);

In my AlertReceiver class, createNotification method, right after I put the extras in my intent.

I know that the best method is to use: PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

with: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

However no matter how many different solutions I tried, I could not get the text to update if I had the first notification detail open and I clicked to open the second notification detail while the application was originally closed.