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);
}
}
}