我正在尝试使用android中的Parse向一个特殊的设备发送通知。这是我的ParseInstallation代码:
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("device_id", "1234567890");
installation.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Log.d(TAG, "done1: "+e);
}
});这是我的代码,用于将通知发送到我已经安装的特定设备:
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("device_id", "1234567890");
ParsePush push = new ParsePush();
push.setQuery(query);
push.setMessage("salamm");
push.sendInBackground(new SendCallback() {
@Override
public void done(ParseException e) {
Log.d(TAG, "done: "+e);
}
});我在日志中得到这样的错误: done: com.parse.ParseRequest$ParseRequestException: unauthorized:需要主密钥
有人能帮我吗?
发布于 2019-05-09 01:31:37
出于安全考虑,不建议直接从前端发送推流。想象一下,一个黑客可以向你所有的客户群发送一条糟糕的消息。
推荐的方法是:-创建一个发送推送的云代码函数- Android应用程序将调用这个云代码函数
这是您的云代码函数应该是这样的:
Parse.Cloud.define('sendPush', function(request, response) {
const query = new Parse.Query(Parse.Installation);
query.equalTo('device_id', request.params.deviceId);
Parse.Push.send({
where: query,
data: {
alert: request.params.message
}
},
{ useMasterKey: true }
)
.then(function() {
response.success();
}, function(error) {
response.error(error);
});
});这是您的客户端代码应该是这样的:
HashMap<String, String> params = new HashMap();
params.put("deviceId", "1234567890");
params.put("message", "salamm");
ParseCloud.callFunctionInBackground("sendPush", params, new
FunctionCallback<Object>() {
@Override
public void done(Object result, ParseException e) {
Log.d(TAG, "done: "+e);
}
});发布于 2019-05-09 01:20:46
Parse Server mo不再支持客户端推送,因为这是一个重大的安全风险。最好的替代方案是将此逻辑放在云代码函数中,并通过Android SDK调用该函数。
有关更多信息,请参阅sending push notification in the JS guide小节。
记得添加use {useMasterKey:true}
https://stackoverflow.com/questions/56036544
复制相似问题