在后台配置点击FCM推送

时间:2017-07-31 18:15:47

标签: android firebase firebase-cloud-messaging

我在使用FCM click_action开始后台活动时遇到了问题。

我的宣言:

<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>

我的讯息设置:

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());

        }

    }

如果我试图发送这个json模型,它总是打开活动主要。如何在fourground和background中操作通知单击以打开ActivityA和ActivityB并需要传递id?...

1 个答案:

答案 0 :(得分:1)

click_action是一个应在notification消息有效内容中使用的参数,不是 data

尝试将有效负载编辑为:

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

请参阅FCM HTTP参数以供参考: https://firebase.google.com/docs/cloud-messaging/http-server-ref