我无法使用解析在安装表中设置设备令牌
我正在使用
ParseInstallation installation =ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId",senderId);
installation.put("pushType","gcm");
installation.put("deviceToken",token);
但是当我尝试使用保存时,我得到了一个例外.
Cannot modify `deviceToken` property of an _Installation object. android
问题是,后端使用此tokenId为另一个提供程序(OneSignal)发送推送通知,所以我想知道它是否可以在deviceToken行中写入(据我所知这只适用于iOS).
我需要在deviceToke中写入我收到的GCM令牌.
谢谢
最佳答案 我通过添加一个名为setDeviceToken的Parse Cloud函数解决了这个问题.这样我就可以使用MasterKey来修改安装记录.
Parse.Cloud.define("setDeviceToken", function(request, response) {
var installationId = request.params.installationId;
var deviceToken = request.params.deviceToken;
var query = new Parse.Query(Parse.Installation);
query.get(installationId, {useMasterKey: true}).then(function(installation) {
console.log(installation);
installation.set("deviceToken", deviceToken);
installation.save(null, {useMasterKey: true}).then(function() {
response.success(true);
}, function(error) {
response.error(error);
})
}, function (error) {
console.log(error);
})
});
然后,在我的Android应用程序的Application.onCreate中:
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@Override
public void idsAvailable(String userId, String registrationId) {
final ParseInstallation currentInstallation = ParseInstallation.getCurrentInstallation();
if (registrationId != null) {
HashMap<String, Object> params = new HashMap<>(2);
params.put("installationId", currentInstallation.getObjectId());
params.put("deviceToken", registrationId);
ParseCloud.callFunctionInBackground("setDeviceToken", params, new FunctionCallback<Boolean>() {
@Override
public void done(java.lang.Boolean success, ParseException e) {
if (e != null) {
// logException(e);
}
}
});
}
}
});