2

Good Morning,

I've made an application that implements GpS Location. I have a service that save my location on LocationChanged event. To avoid that Android close my app I start a notification and all works well. But now, I want that when I click on notification from Action Bar the app come back in foreground.I use fragment and the map fragment is called MappaFragment. I read a lot of messages but It seems not resolve my issue. Below my code, any suggestion is appreciated ! Alex This is my monitoring Service:

    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent.getAction().equals(Constants.STARTFOREGROUND_ACTION)) {
            Log.i(TAG, "Received Start Foreground Intent ");
            Intent notificationIntent = new Intent(getApplicationContext(), LocationMonitoringService.class);

            Bundle extras = intent.getExtras();

            numeroTraccia = extras.getInt("numeroTraccia");

            itinerario = extras.getString("itinerarioRiferimento");

            notificationIntent.setAction(Constants.MAIN_ACTION);

            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            Intent.FLAG_ACTIVITY_CLEAR_TASK);

            setGiornoRiferimento(gg.checkDayLong(this));

            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
                0, new Intent(getBaseContext(),MappaFragment.class), 0);
            Bitmap icon =  BitmapFactory.decodeResource(getResources(), com.example.alex.myApp.R.drawable.smallicon );

            Notification.BigTextStyle bigText = new Notification.BigTextStyle();

            Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("MyApp")
                .setTicker(getString(R.string.track))
                .setStyle(bigText.bigText(getString(R.string.track)))
                .setSmallIcon(R.drawable.smallicon)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .build();
         startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                notification);

}

This is my Manifest:

    <activity android:name="com.example.alex.myApp.MainActivity"
        android:screenOrientation="portrait"
    android:configChanges="orientation|screenSize|keyboardHidden" />

    <activity
        android:name="com.example.alex.myApp.SplashScreen"
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name="com.example.alex.myApp.services.MonitoringService"
        android:enabled="true"/>
    <receiver
        android:name="com.example.alex.myApp.RestarterBroadcastReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="RestartWhenStopped">
        <intent-filter>
            <action android:name="com.example.alex.myApp.ActivityRecognition.RestartSensor"/>
        </intent-filter>
    </receiver>

Thanks in advance ! Alex

Magobin
  • 93
  • 2
  • 13
  • You cant launch a fragment from an intent, you need to launch the activity that holds the fragment – tyczj Feb 09 '18 at 15:38
  • so...I changed in: PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MainActivity.class), 0); but in this case...it launch a new Activity! – Magobin Feb 09 '18 at 15:41
  • yeah that is correct its going to launch that activity send information on your bundle to tell the activity what you want it to do then use the bundle in your activity to do the stuff – tyczj Feb 09 '18 at 15:43
  • mmm I'm not sure that I understood well. I see that even if Activity is a new Activity, monitoring service continue to run, and this is correct. The problem is that new Activity doesn't know nothing about it. Basically I have to stop monitoring service when new Activity is launched...in this case. I thought that clicking on Notification message the app came back in foreground ! – Magobin Feb 09 '18 at 15:50

2 Answers2

2

First of all,

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getBaseContext(),MappaFragment.class), 0);

this is incorrect. Fragment can't be opened using intent. You need to launch an activity, then redirect to required fragment.

Next thing is, if app is in background and you click on the notification, it will open launcher activity. So in launcher activity you need to check for bundle object and there you can redirect to particular activity or fragment. Check this post: Push notification works incorrectly when app is on background or not running and Open specific Activity when notification clicked in FCM it will help you to better understand how FCM works when app is in background and foreground.

Srushti
  • 131
  • 7
0

Create a BroadCast Receiver like below:

public static class ActionReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    if (action.equals("open_activity")) {
      intent.setAction(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      intent.setClass(AppContext.getContext(), MappaFragment.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
      context.startActivity(intent);
    }
  }
}

And then, Use this notificationIntent.setAction("open_activity");

And Change your PendingIntent to:

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
                0, new Intent(AppContext.getContext(), ActionReceiver.class), 0);

NOTE: For USing This you need to Remove These Lines:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification)

What I'm suggesting is to use a BroadcastReceiver to receive whatever you want to do in Your Notification.

Also, If this isn't working try Adding .setAction(Intent.ACTION_MAIN) to Your Notification like Below:

Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("MyApp")
                .setTicker(getString(R.string.track))
                .setStyle(bigText.bigText(getString(R.string.track)))
                .setSmallIcon(R.drawable.smallicon)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setAction("open_activity")
                .build();
Shahin
  • 391
  • 3
  • 9