Reputation: 73
I want to open a specific tab, based on code. It works if I do this via a normal button.
The way it works:
public void toSomewhere (View view) {
Intent intent = new Intent(this, SomewhereActivity.class);
intent.putExtra("FirstTab", 2);
startActivity(intent);
}
Intent i = getIntent();
int tabToOpen = i.getIntExtra("FirstTab", -1);
if (tabToOpen!=-1) {
// Open the right tab
}
else {
// Other tab
}
The way I want it to work through notifications (at this moment I have this code which sends a notification, but doesn't give the .putExtra through):
public static void NotificationIntent(String title, String message) {
Intent notificationIntent = new Intent(currentContext, SomewhereActivity.class);
**notificationIntent.putExtra("FirstTab", 2);**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(currentContext, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(currentContext)
.setContentTitle(title)
.setContentIntent(intent)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) currentContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
Does anyone know how to fix this, so it will work also with a notification post?
Upvotes: 1
Views: 4687
Reputation: 15821
Use an FLAG_UPDATE_CURRENT
From docs :
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
Code :
Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);
notificationIntent.putExtra("NotificationMessage", notificationMessage);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);
In Activity in onNewIntent
method :
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String notificationMessage = extras.getString("NotificationMessage", "UNDEFINED");
}
also forward call in onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
Upvotes: 8