FCM Open Activity and call fragment when the appli

2019-04-17 10:28发布

问题:

I'm trying to open a fragment initialized in an Activity through Firebase Cloud Messaging.

The fragment call works when the application is open, but once I close the application the push notification only opens the Activity and not the fragment.

MyActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //All my initial code

        Intent i = getIntent();
        Bundle extras = i.getExtras();

        if(extras != null) {
            String push = extras.getString("push");
            if (push != null) {
                Integer id = Integer.parseInt(extras.getString("id"));
                goToDetalleDenuncia(id);
            }
        }else
            goToMenu();
    }

What I can do to debug (or resolve) this issue? I can't use Logcat (at least using eclipse) because the application is closed at this point

回答1:

Finally, I found a solution for my issue.

Well, first one, the FCM notification can't call a fragment directly. You must use the click_action parameter in your intent.

First one Manifest.xml

<activity
            android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar">
            <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
            <intent-filter>
                <action android:name="sm.boson.com.smd.MAIN" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

Here, you must add in the intent-filter section an action android:name and specify a name. In my case I used the same package name follow of ".MAIN" (Symbolizing the MainActivity but you could use some other name).

MyFirebaseMessagingService.java

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String id = remoteMessage.getData().get("tag") ;

        String click_action = remoteMessage.getNotification().getClickAction();

        Intent intent = new Intent(click_action);
        intent.putExtra("id", id);
        intent.putExtra("push", "true");

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());

        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }

In the declaration of my Intent I added Extra to send some parameters to the activity.

MyActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //ALL THE LOGIC OF YOU ONCREATE EVENT

        Intent i = getIntent();
        Bundle extras = i.getExtras();

        if(extras != null) {
            String push = extras.getString("push");
            if (push != null) {
                Integer id = Integer.parseInt(extras.getString("id"));
                goToDetalleDenuncia(id);
            }else if (  extras.getString("tag") != null  ){
                Integer id = Integer.parseInt(extras.getString("tag"));
                goToDetalleDenuncia(id);
            }else
                goToMenu();
        }else
            goToMenu();
    }

Well, when the application is running, you can get the parameter with the first if statement and get them with: extras.getString("push")

BUT, if your application is running in background (or it's closed) you must get the paramters with:

Integer.parseInt(extras.getString("tag"));

Why "tag"? In my MyFirebaseMessagingService I get the "tag" and I put it into an "id" key in Extra for my Intent.

And my json from my server like:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "priority" : "normal",
  "notification" : {
    "body" : "This week's edition is now available.",
    "title" : "Mytitle",
    "icon" : "new"
  },
  "data" : {
    "tag" : "3.21.15",
  }
}

And in this way, now I can open an specific Fragment using FCM when the application is running, when is in background and when it's closed.

Hope this helps you.