I'm trying to use PendingIntent
in the same activity. If the MainActivity
is already running and the app receives a notification, when this notification is clicked it should load the TestFragment
Class but that is not the case here, When notification is clicked nothing happens.
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("NOTIFICATION", "notify");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(mContext)
.setContentIntent(pendingIntent)
.setContentTitle(title)
.setSmallIcon(R.drawable.logo)
.setLargeIcon(bitmap)
.setPriority(Notification.PRIORITY_MAX)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
But when I just use this code for the Intent
and PendingIntent
, I get to load the TestFragment
however it creates the activity twice. It loads the TestFragment on top of the default view.
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("NOTIFICATION", "notify");
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
MainActivity.class onCreate()
String notificationIntent = getIntent().getStringExtra("NOTIFICATION");
if(notificationIntent != null) {
if (notificationIntent.equals("notify")) {
TestFragment fr = new TestFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fr);
fragmentTransaction.commit();
}
} else {
//default view
}
Constant to pass the extra
private static final String NOTIFICATION = "notification";
Notification intent
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra(NOTIFICATION, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent =
PendingIntent.getActivity(mContext, 100, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
In MainActivity.class added onNewIntent()
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
and onResume()
@Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if (extras != null) {
final boolean fromNotification = extras.getBoolean(NOTIFICATION);
if (fromNotification) {
TestFragment fr = new TestFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fr);
fragmentTransaction.commit();
}
}
}
AndroidManifest.xml
<activity
android:name="test.com.sample.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>