我创建了一个用户可以在其他用户中发送消息(通知)的应用程序。这些perpose我使用解析SDK。所以我使用以下代码将消息从设备发送到解析云。
final ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("email", "user@email.com");
query.getFirstInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser object, ParseException e) {
if (e == null) {
Toast.makeText(getActivity(), "User found", Toast.LENGTH_SHORT).show();
String search_username = object.getString("username");
String id = object.getObjectId();
Log.d("ObjectID:",id);
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("recipientId", id);
params.put("message", username);
ParseCloud.callFunctionInBackground("sendPushToUser", params, new FunctionCallback<String>() {
public void done(String success, ParseException e) {
if (e == null) {
// Push sent successfully
Toast.makeText(getActivity(), "Request send", Toast.LENGTH_SHORT).show();
}
}
});
然后我有下一个云功能接收并将消息推送给特定用户。
Parse.Cloud.define("sendPushToUser", function(request,response){
var senderUser = request.user;
var recipientUserId = request.params.recipientId;
var message = request.params.message;
var title ="Friend Request";
if(message.length > 140){
message = message.substring(0, 137) + "...";
}
var recipientUser = new Parse.User();
recipientUser.id = recipientUserId;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", recipientUser);
Parse.Push.send({
where: pushQuery,
data: {
"alert":{"data":{"message":"message",
"title":"title"}}
}
}).then(function(){
response.success("true")
}, function(error) {
response.error("Push failed to send with error: "+error.message);
});
});
但这条消息从未收到过。如果我从解析仪表板发送推送通知,一切正常。谁知道怎么解决?设备期望收到JSON,那么我的云功能是不是以json格式发送数据?提前致谢
答案 0 :(得分:1)
我发送通知时遇到了两件事
用户登录您的应用后,通过
将其ID添加到安装中ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.addUnique("userId", currentUser.getObjectId());
installation.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
// check the returned exception
if (e == null) {
// everything worked fine
} else {
// error occurred
}
}
});
希望它有所帮助:)
<强>更新强>
在您的代码中,您发送收件人userId虽然您在云功能中也保存了用户名,但您遇到了同样的问题,用户名已保存,但您根据ID查询安装。我已经更新了上面的安装,也改变了用户&#34;在您的云功能中使用&#34; userId&#34;
pushQuery.equalTo("userId", recipientUser);