gpt4 book ai didi

android - 在 Android 解析中设置 DeviceToken

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:15:03 25 4
gpt4 key购买 nike

我无法使用解析在安装表中设置设备 token 我正在使用

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 token 。谢谢

最佳答案

我通过添加名为 setDeviceToken 的 Parse Cloud 函数解决了这个问题。这样我就可以使用万能 key 修改安装记录了。

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);
}
}
});
}
}
});

关于android - 在 Android 解析中设置 DeviceToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37799656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com