Reputation: 298
I am working on notification part. I am able to navigate user to activity onclick of Notification message. My actual task is to navigate user to fragment.
Here is the notification code which works fine.
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Lugmah Order Status")
.setContentText("The Order Status of Order Id: "+selectedOrderId+ " is: "+status_desc)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setAutoCancel(true);
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(getApplicationContext(), MainActivity.class);
targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
targetIntent.putExtra("isTrackOrder", false);
targetIntent.putExtra("orderNotification", "orderNotification");
targetIntent.putExtra("isLoggedIn", true);
targetIntent.putExtra("status_desc", status_desc);
targetIntent.putExtra("selectedOrderId", selectedOrderId);
PendingIntent contentIntent = PendingIntent
.getActivity(getApplicationContext(), 0, targetIntent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
I have added MainActivity.java in intent because that is the place I load all fragments. In this activity, I did something like this.
Here is the code inside onCreate():
String orderNotification = getIntent().getStringExtra("orderNotification");
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (orderNotification != null)
{
if (orderNotification.equals("orderNotification"))
{
TrackOrderFragment trackOrderFragment = new TrackOrderFragment();
fragmentTransaction.replace(android.R.id.content, trackOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Help would be appreciated.
Upvotes: 1
Views: 912
Reputation: 288
Your notification code works fine you need to adjust a bit the code onCreate.
if(getIntent().getExtras() != null) {
String orderNotification = getIntent().getStringExtra("orderNotification");
if (orderNotification.equals("orderNotification"))
{
TrackOrderFragment trackOrderFragment = new TrackOrderFragment();
fragmentTransaction.replace(android.R.id.content, trackOrderFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
You need to check if you are receiving any extras every time your activity runs onCreate because a null pointer exception can occur.
Upvotes: 4