Firebase推送通知从模型类发送额外的数据

时间:2019-02-11 11:44:31

标签: java android firebase

我正在使用FirebaseMessagingService使用我的应用程序向其他用户发送通知,问题是我无法向消息中添加额外的数据。我有一个通知,发送者和数据模型类。问题是,当我发送令牌时尝试添加数据模型时,却收到无效的JSON。

通知类别:

var array = [1,3,2,1,4,3,99,3,5,2,1,45]

extension Array where Element == Int {

    mutating func replace(difference: Int) {
        guard count > 0 else { return }
        self = [self[0]] + self.dropFirst().enumerated().map {
            let condition = abs(self[$0.offset] - $0.element) > difference
            if condition { self[$0.offset + 1] = 0 }
            return condition ? 0 : $0.element
        }
    }

}

array.replace(difference: 10)
print(array) // [1,3,2,1,4,3,0,3,5,2,1,0]

发件人类别:

    public String title;
    public String body;


    public Notification() {
    }

    public Notification(String title, String body) {
        this.title = title;
        this.body = body;

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}

数据类:

public class Sender {

    public Notification data;
    public data extra;
    public String to;

    public Sender() {
    }

    public Sender(Notification data, data extra, String to) {
        this.data = data;
        this.to = to;
        this.extra = extra;
    }

    public Notification getData() {
        return data;
    }

    public void setData(Notification data) {
        this.data = data;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }
}

首页:

public class data {

    public String username;

    public data() {
    }

    public data(String username) {
        this.username = username;

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

MyFirebaseMessaingService:

                    data extra = new data("Test");
                    Notification notification = new Notification("title test", "body test");
                    Sender content = new Sender(notification, extra, token.getToken());

                    mService.sendMessage(content)
                            .enqueue(new Callback<FCMResponse>() {
                                @Override
                                public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                                    Log.i(TAG, "onResponse: " + response.toString());
                                    if (response.body().success == 1){
                                        Toast.makeText(c, "Request sent!", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(c, "Request failed!", Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onFailure(Call<FCMResponse> call, Throwable t) {
                                    Log.e(TAG, "onFailure: "+ t.getMessage());
                                }
                            });

当我从模型类发送额外的数据(即用户名)时出现问题。不发送额外的数据,即仅发送通知就可以正常工作。

0 个答案:

没有答案