I've got a navigation drawer app set up that loads fragments, I've also implemented Parse.com push notifications. I receiver the notifications via custom ParsePushBroadcastReceiver . What I want now is to go to a specific fragment in my navigation drawer when I click on a push notification. I've tried countless ways but non seem to load other than the first fragment. I know I have to launch my MainActivity first, pass an intent on push-click and then check in my activity class for that intent (although I'm not sure if I should check under my onCreate method or some newPushIntent method).
Here is my Custom ParsePushBroadcastReceiver class:
public class Receiver extends ParsePushBroadcastReceiver {
public static final String PARSE_DATA_KEY = "com.parse.Data";
@Override
protected void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
if (intent == null)
return;
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
try {
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
String message = data.getString("message");
showNotificationMessage(context, title, message);
} catch (JSONException e) {
}
} catch (JSONException e) {
}
}
@Override
protected void onPushDismiss(Context context, Intent intent) {
super.onPushDismiss(context, intent);
}
@Override
protected void onPushOpen(Context context, Intent intent) {
super.onPushOpen(context, intent);
JSONObject pushData = null;
try {
pushData = new JSONObject(intent.getStringExtra(KEY_PUSH_DATA));
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("store", pushData.getString("data"));
context.startActivity(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Shows the notification message in the notification bar
* If the app is in background, launches the app
*
* @param mContext
* @param title
* @param message
*/
private void showNotificationMessage(Context mContext, String title, String message) {
// notification icon
int icon = R.mipmap.ic_launcher; //This used to be MainActivity.class
Intent intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setStyle(inboxStyle)
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(101, notification);
}
}
Here is my MainActivity.java onCreate method where I checked for an intent:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Intent intent = this.getIntent();
if (intent != null) {
String menuFragment = getIntent().getStringExtra("store");
if (menuFragment != null) {
com.rayyan_nasr.apple.besgb.activity.Parse favoritesFragment = new com.rayyan_nasr.apple.besgb.activity.Parse();
fragmentTransaction.replace(R.id.container_body, favoritesFragment);
fragmentTransaction.commit();
} else {
MainFragment standardFragment = new MainFragment();
fragmentTransaction.replace(R.id.container_body, standardFragment);
fragmentTransaction.commit();
}
}
In addition to ensuring that you get the correct Intent Extra, make sure that you call commit() on the FragmentTransaction:
String menuFragment = getIntent().getStringExtra("store"); //changed to "store"
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (menuFragment != null)
{
if (menuFragment.equals("favoritesMenuItem"))
{
com.rayyan_nasr.apple.besgb.activity.Parse favoritesFragment = new com.rayyan_nasr.apple.besgb.activity.Parse();
fragmentTransaction.replace(R.id.container_body, favoritesFragment);
fragmentTransaction.commit(); //added
}
}
else
{
HomeFragment standardFragment = new HomeFragment();
fragmentTransaction.replace(R.id.container_body, standardFragment);
fragmentTransaction.commit(); //added
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.