Configure click on the FCM push in the background

2019-08-18 01:59发布

问题:

I'm having trouble starting a background activity using FCM click_action.

My manifesto:

<activity
            android:name="com.myoro.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"/>

          <activity
            android:name="com.myoro.ActivityA"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.myoro.A" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.myoro.ActivityB"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.myoro.B" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

My message settings:

data: {{"type" : "A","id" : "a123","click_action": "com.myoro.A"}}

JAVA:

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

        if (remoteMessage.getData().size() > 0) {


            if (remoteMessage.getData().containsKey("click_action")) {

                Intent intent = new Intent(remoteMessage.getData().get("click_action"));            
                intent.putExtra("id", remoteMessage.getData().get("id"));
                startActivity(intent);            
                return;

            }

            if (remoteMessage.getData().containsKey("data")) {

                 String data = remoteMessage.getData().get("data");
                 Gson gson = new Gson();
                 MyObj myObj = gson.fromJson(data, MyObj.class);

                 if(myObj.getType().equals("A")) {

                     Intent intent = new Intent(context, ActivityA.class);            
                     intent.putExtra("id",  myObj.getId());
                     startActivity(intent);          

                 } else {

                     Intent intent = new Intent(context, ActivityB.class);            
                     intent.putExtra("id", myObj.getId());
                     startActivity(intent);          

                 }

            }


        }

        if (remoteMessage.getNotification() != null) {

            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        }

    }

In case I am trying to send this json model and it is always opening the activity main. How can I manipulate the notification click in fourground and background to open the ActivityA and ActivityB and need to pass the id?...

回答1:

click_action is a parameter that should be used inside a notification message payload, not data.

Try to edit your payload to something like:

}
    notification:{
        "click_action": "com.myoro.A"
    },
    data: {
        "type" : "A",
        "id" : "a123"
    }
}

See the FCM HTTP parameters for reference: https://firebase.google.com/docs/cloud-messaging/http-server-ref