2

hi how to handle push notification click i need to open activity with specific fragment , i have two states like app is closed and app is opened , when the app is closed i need to open home activity with MyFragment.class , if app is opned then i need to show dialog u need to see details if yes i need to open specific fragment like MyFragment, if no i need to dissmis dialog, ,, how to implemnet it...

@SuppressWarnings("deprecation")
    @Override
    public void update(java.util.Observable observable, Object data) {

        NotificationManager notificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();
        Notification notification = new Notification(R.drawable.ic_launcher,
                data.toString(), when);

        String title = this.getString(R.string.app_name);

        Intent notificationIntent = new Intent(this, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(this, title, data.toString(), intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

        Log.d("GCM_TOKEN", data.toString());

        Toast.makeText(this, "Error: " + data.toString(), Toast.LENGTH_LONG)
                .show();

    }

thanks

venu
  • 2,971
  • 6
  • 40
  • 59

1 Answers1

0

I've implemented this a few days ago. First I started to determine the active and inactive state with a static variable called isRunning. In the Activities onStart()-Method I set isRunning to true, and when the Activity goes into stop, set the variable to false. So you can determine the active and inactive state.

@Override
public void onStart() { 
super.onStart(); 
mIsRunning = true; 
}

 @Override
public void onStop() { 
super.onStop(); 
mIsRunning = false; 
}

According to this variable you can display your notifications as desired ;-)

jennymo
  • 1,450
  • 1
  • 18
  • 43