2

I am trying to open a fragment when I press a notification in the notification bar. My app structure is:

  • a base activity with a nav drawer menu
  • and some fragment that are opened from the menu

When I press the notification the activity reopens but not in the indicated fragment, and in the LogCat the data from the indicated fragment looks to open and load but not with the UI.

The notification code :

       NotificationCompat.Builder  mBuilder = 
    new NotificationCompat.Builder(this);   
      mBuilder.setContentTitle("Comanda Noua");
      mBuilder.setContentText("S-a introdus o comanda noua");
      mBuilder.setTicker("Comanda noua!");
      mBuilder.setSmallIcon(R.drawable.calculator_icon);
     mBuilder.setAutoCancel(true);//inchide notificare dupa ce s-a dat click pe ea

      //creste numar notificare
      mBuilder.setNumber(++numMessages);

      // cream un intent 
      Intent resultIntent = new Intent(this, MainActivity.class);

      //am setat actiune pentru a deschide fragmentul corespunzator notificartii la apasare
      resultIntent.setAction("Action1");
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
      stackBuilder.addParentStack(MainActivity.class);


      // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent);
      PendingIntent resultPendingIntent =
         stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

      mBuilder.setContentIntent(resultPendingIntent);

      mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

      // notificationID allows you to update the notification later on. 
      mNotificationManager.notify(notificationID, mBuilder.build());

The code where I open the fragment when notification is pressed:

 Intent intent = getIntent();
   try{
        String action = intent.getAction();
       // Log.d("action:",action);

        if(action.equals("Action1")){
             //Log.d("deschidem agenda:",action);
            AgendaFragment fragment = new AgendaFragment();
         FragmentTransaction transaction = getFragmentManager()
                    .beginTransaction();
         transaction.replace(R.id.frame_container, fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .addToBackStack(null).commit();
        }else{
           Log.d("eroare", "Intent was null");
        }
   }catch(Exception e){
        Log.e("eroare", "Problem consuming action from intent", e);             
    } 

Why the activity open but the fragment not?

user3481497
  • 23
  • 1
  • 7
  • Why do You using TaskStackBuilder to create pending intent? – Roman Black Jun 25 '14 at 09:03
  • I am new in Android, and the notification code i have from internet. I think i use that to put the activity in the stack.if i did not use that the notification does nothing – user3481497 Jun 25 '14 at 11:43

2 Answers2

0

Try below code, it is more simple way to show notification:

Intent resultIntent = new Intent(this, MainActivity.class);

resultIntent.setAction("Action1");

PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.icon_notification, "Your title", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, "Your title", "Your text", pendingIntent);

mNotificationManager.notify(notificationID, notification);
Roman Black
  • 3,501
  • 1
  • 22
  • 31
  • I have this error: The method getActivity(Context, int, Intent, int) is undefined for the type MainActivity – user3481497 Jun 25 '14 at 12:12
  • The notification appears but when i click on it the main activity starts but not the specific fragment(the fragment loads in LogCat but the UI didn`t appear). And after i navigate back from the new instance activity from the notification another notification appears... – user3481497 Jun 25 '14 at 12:29
  • I retest. The notification starts my activity but not oppens my fragment. In LogCat the fargment appears to load but the UI did not appear on the screen, instead of that i have just the activity . – user3481497 Jun 25 '14 at 12:41
0

TRY with broadcast

In notification class you have to sendBroadcast. its default method.

        `intent.setAction("one");
        sendBroadcast(intent);`

In Base Activity just create Broadcast reveiver

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent!=null && intent.getAction().equals("one")) 

        {
            displayView(fragement_position);
        }
    }
};

In on create of BaseAcitivty resiger the broatcast receiver

  IntentFilter filter = new IntentFilter();
    filter.addAction("one");
    registerReceiver(getList, filter);

You must add intentFilter

and you must unRegister in on Destroy of base Activity

@Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); }

John
  • 1,407
  • 7
  • 26
  • 51