如何使用FCM在android中将令牌配置为令牌通知?

时间:2019-10-11 08:14:46

标签: java android firebase notifications firebase-cloud-messaging

我正在尝试使用android中的FCM找出用户到用户的通知呼叫, 我有一个“ subscribeToTopic(” TOPIC Name“)”的示例,但与此相反,我无法弄清楚如何使用FCM中用户的令牌ID将用户应用于用户通知。 我指的是本教程https://blog.usejournal.com/send-device-to-device-push-notifications-without-server-side-code-238611c143

请协助,在此先感谢

btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TOPIC = "/topics/userABC"; //topic must match with what the receiver subscribed to
                NOTIFICATION_TITLE = edtTitle.getText().toString();
                NOTIFICATION_MESSAGE = edtMessage.getText().toString();

                JSONObject notification = new JSONObject();
                JSONObject notifcationBody = new JSONObject();
                try {
                    notifcationBody.put("title", NOTIFICATION_TITLE);
                    notifcationBody.put("message", NOTIFICATION_MESSAGE);

                    notification.put("to", TOPIC);
                    notification.put("data", notifcationBody);
                } catch (JSONException e) {
                    Log.e(TAG, "onCreate: " + e.getMessage() );
                }
                sendNotification(notification);
            }
        });

btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TOPIC = "/topics/userABC"; //topic must match with what the receiver subscribed to
                NOTIFICATION_TITLE = edtTitle.getText().toString();
                NOTIFICATION_MESSAGE = edtMessage.getText().toString();

                JSONObject notification = new JSONObject();
                JSONObject notifcationBody = new JSONObject();
                try {
                    notifcationBody.put("title", NOTIFICATION_TITLE);
                    notifcationBody.put("message", NOTIFICATION_MESSAGE);

                    notification.put("to", TOPIC);
                    notification.put("data", notifcationBody);
                } catch (JSONException e) {
                    Log.e(TAG, "onCreate: " + e.getMessage() );
                }
                sendNotification(notification);
            }
        });

1 个答案:

答案 0 :(得分:0)

使用这种JSON格式将通知发送到特定设备。

{ "notification": {
    "title": "Title Message",
    "text": "Message here"
  },
  "to" : "YOUR_TARGET_device_token_id"
}