【问题标题】:Configure click on the FCM push in the background配置点击后台FCM推送
【发布时间】:2017-07-31 18:15:46
【问题描述】:

我在使用 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?...

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多