Firebase云消息传递FCM发送通知消息Android

时间:2018-03-28 15:50:44

标签: android firebase notifications firebase-cloud-messaging

我有两个使用FCM,Client和Worker应用的应用。 客户 正在发送消息:

String jsonLatLng = new Gson().toJson(new LatLng(Common.placeLatLng.latitude, Common.placeLatLng.longitude));
String clientToken = FirebaseInstanceId.getInstance().getToken();
Notification notification = new Notification(clientToken, jsonLatLng);
Sender content = new Sender(tokenId, notification);

mFCMService.sendMessage(content)
        .enqueue(new Callback<FCMResponse>() {
            @Override
            public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                if(response.body().success == 1) {
                    Toast.makeText(HomeActivity.this, "Request sent.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(HomeActivity.this, "Request not sent.", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<FCMResponse> call, Throwable t) {

            }
});

其中 Notification.java

public class Notification {
    public String title;
    public String body;
    ...
}

Sender.java

public class Sender {
    public String to;
    public Notification notification;
    ...
}

使用 Worker 应用,它会收到:

public class MyFirebaseMessaging extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Convert lat lng
        LatLng clientLocation = new Gson().fromJson(remoteMessage.getNotification().getBody(), LatLng.class);

        Intent intent = new Intent(getBaseContext(), NotificationActivity.class);
        intent.putExtra("lat", clientLocation.latitude);
        intent.putExtra("lng", clientLocation.longitude);
        intent.putExtra("client", remoteMessage.getNotification().getTitle());

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

这些代码完全正常,但是,我需要添加更多细节,具体来说,我想从2个字符串变量发送数据, serviceFee &amp; serviceType 到工作者应用程序。我尝试修改Notification的主体,其中我创建了一个名为 Body 的类,其中包含三个变量(jsonLatLng,serviceFee,serviceType),但我无法弄清楚工人将如何获得Body的数据或者甚至是可能的。请帮忙。谢谢! :)

0 个答案:

没有答案