我无法使用parse im在安装表中设置设备令牌
ParseInstallation installation =ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId",senderId);
installation.put("pushType","gcm");
installation.put("deviceToken",token);但是当我尝试使用save时,我得到了一个异常。
Cannot modify `deviceToken` property of an _Installation object. android问题是,后端使用这个tokenId为另一个提供者(OneSignal)发送推送通知,所以我想知道是否有任何方法可以写入deviceToken行(据我所知,这只适用于iOS )。我需要在deviceToke中写入我收到的GCM令牌。谢谢
发布于 2017-01-21 03:40:56
我通过添加一个名为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);
})
});然后,在我的安卓应用的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);
}
}
});
}
}
});发布于 2017-04-04 10:28:18
对于将来的访问者,我使用Parse Rest API以一种不同的方式解决了这个问题:http://parseplatform.org/docs/rest/guide/
使用它,您可以绕过常规的SDK限制。只需向https://yourApp/parse/installtions/{installationObjectIDHere}发出put请求。制作一个字典并将其编码为JSON:
Map<String, String> dictionary = new HashMap<String, String>();
dictionary.put("deviceToken", yourToken);
JSONObject jsonDictionary = new JSONObject(dictionary);
String bodyAsString = jsonDictionary.toString();
RequestBody body = RequestBody.create(JSON, bodyAsString);然后发送请求,这应该是可行的。
发布于 2018-04-04 20:50:05
joey展示了这样做的正确方法,我添加的额外部分是关于身份验证头的。这是我所做的:
val JSON = MediaType.parse("application/json; charset=utf-8")
val client = OkHttpClient()
val installationObjectId = ParseInstallation.getCurrentInstallation().objectId
val myAppId = getString(R.string.parse_app_id)
val key = getString(R.string.parse_client_key)
val server = getString(R.string.parse_server_url)
val mRequestUrl = "$server/installations/$installationObjectId"
val dictionary = HashMap<String, String?>()
dictionary["deviceToken"] = myToken
val jsonDictionary = JSONObject(dictionary)
val bodyAsString = jsonDictionary.toString()
val body = RequestBody.create(JSON, bodyAsString)
val request = Request.Builder()
.url(mRequestUrl)
.put(body)
.addHeader("X-Parse-Application-Id", myAppId)
.addHeader("X-Parse-Client-Key", key)
.build()
client.newCall(request).execute()https://stackoverflow.com/questions/37799656
复制相似问题