解析推送通知从服务器发送但不被客户端接收
Parse Push Notifications are sent from a server but not received by a client
我的目标是让一个用户向另一个用户发送推送通知。
为了触发我的推送通知,我将一些数据加载到哈希映射中,并将其发送到一些云代码,然后云代码将验证数据并将其转发给目标用户。我检查了服务器日志,我可以验证 Cloud Code 是否正确接收数据并发送成功请求,但是,我的广播接收器从未接收到发送给目标用户的数据。我确实测试过,如果我直接从 Parse 发送推送通知,通过 facebookId 过滤,那么设备将收到推送通知。
我遵循了这个教程:http://blog.parse.com/learn/engineering/the-dangerous-world-of-client-push/
这是我的客户端发送代码:
ParseQuery<ParseUser> parseQuery = ParseUser.getQuery();
parseQuery.whereEqualTo("facebookId", userId);
HashMap<String, Object> params = new HashMap<String, Object>();
try {
ParseUser matchUser = parseQuery.getFirst();
params.put("name", matchUser.getString("name"));
params.put("recipientId", matchUser.getObjectId());
params.put("facebookId", matchUser.getString("facebookId"));
params.put("startDate", new Date(matchEvent.getLong("startDate")).toString());
params.put("endDate", new Date(matchEvent.getLong("endDate")).toString());
} catch (ParseException pe) {
Log.e("MatchTimesArrayAdapter", "The match user is null");
}
ParseCloud.callFunctionInBackground("validatePush", params, new FunctionCallback<String>() {
@Override
public void done(String s, ParseException e) {
if (e == null) {
// Push sent successfully
Toast.makeText(context, "Request sent successfully", Toast.LENGTH_SHORT).show();
} else {
e.printStackTrace();
}
}
});
这是我的云代码:
Parse.Cloud.define("validatePush", function(request, response) {
var senderUser = request.user;
var senderName = request.params.name;
var recipientId = request.params.recipientId;
var recipientUserId = request.params.facebookId;
var startDate = request.params.startDate;
var endDate = request.params.endDate;
var message = senderName + " would like to meet with you from " + startDate + " to " + endDate + ".";
if (senderUser.get("FriendsList").indexOf(recipientUserId) === -1) {
response.error("The recipient is not the sender's friend, cannot send push");
}
var recipientUser = new Parse.User();
recipientUser.id = recipientId;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", recipientUser);
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Request was sent successfully.");
}), function(error) {
response.error("Push failed to send with error: " + error.message);
};
});
这是我的广播接收器:
public class ExerciseRequestBroadcastReceiver extends ParsePushBroadcastReceiver {
@Override
protected void onPushReceive(Context context, Intent intent) {
int notificationId = 51;
try {
String action = intent.getAction();
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
// replace openAppIntent with event modification
Intent openAppIntent = new Intent(context, MatchesActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MatchesActivity.class);
stackBuilder.addNextIntent(openAppIntent);
PendingIntent acceptIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent declineIntent = null;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_login)
.setContentTitle("Fiternity")
.setContentText(intent.getDataString())
.addAction(android.R.drawable.ic_input_add, "Accept", acceptIntent)
.addAction(android.R.drawable.ic_input_delete, "Decline", declineIntent);
notificationManager.notify(notificationId, notificationBuilder.build());
} catch (JSONException e) {
Log.e("ReceiverIssue", "JSONException: " + e.getMessage());
}
}
}
我有一种预感,它与我在 ParseInstallation 中存储数据的方式有关。我将 facebookId 保存到特定的 ParseInstallation,但我有一个单独的 table,其中包含用户对象。我需要将 ParseUser 对象保存到 ParseInstallation 吗?
非常感谢任何帮助,谢谢!
我放弃了使用 Parse User 或 Parse Installation 作为 ID 的尝试,而是使用 Facebook ID 来获取推送通知。
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("facebookId", recipientId);
我的目标是让一个用户向另一个用户发送推送通知。
为了触发我的推送通知,我将一些数据加载到哈希映射中,并将其发送到一些云代码,然后云代码将验证数据并将其转发给目标用户。我检查了服务器日志,我可以验证 Cloud Code 是否正确接收数据并发送成功请求,但是,我的广播接收器从未接收到发送给目标用户的数据。我确实测试过,如果我直接从 Parse 发送推送通知,通过 facebookId 过滤,那么设备将收到推送通知。
我遵循了这个教程:http://blog.parse.com/learn/engineering/the-dangerous-world-of-client-push/
这是我的客户端发送代码:
ParseQuery<ParseUser> parseQuery = ParseUser.getQuery();
parseQuery.whereEqualTo("facebookId", userId);
HashMap<String, Object> params = new HashMap<String, Object>();
try {
ParseUser matchUser = parseQuery.getFirst();
params.put("name", matchUser.getString("name"));
params.put("recipientId", matchUser.getObjectId());
params.put("facebookId", matchUser.getString("facebookId"));
params.put("startDate", new Date(matchEvent.getLong("startDate")).toString());
params.put("endDate", new Date(matchEvent.getLong("endDate")).toString());
} catch (ParseException pe) {
Log.e("MatchTimesArrayAdapter", "The match user is null");
}
ParseCloud.callFunctionInBackground("validatePush", params, new FunctionCallback<String>() {
@Override
public void done(String s, ParseException e) {
if (e == null) {
// Push sent successfully
Toast.makeText(context, "Request sent successfully", Toast.LENGTH_SHORT).show();
} else {
e.printStackTrace();
}
}
});
这是我的云代码:
Parse.Cloud.define("validatePush", function(request, response) {
var senderUser = request.user;
var senderName = request.params.name;
var recipientId = request.params.recipientId;
var recipientUserId = request.params.facebookId;
var startDate = request.params.startDate;
var endDate = request.params.endDate;
var message = senderName + " would like to meet with you from " + startDate + " to " + endDate + ".";
if (senderUser.get("FriendsList").indexOf(recipientUserId) === -1) {
response.error("The recipient is not the sender's friend, cannot send push");
}
var recipientUser = new Parse.User();
recipientUser.id = recipientId;
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("user", recipientUser);
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Request was sent successfully.");
}), function(error) {
response.error("Push failed to send with error: " + error.message);
};
});
这是我的广播接收器:
public class ExerciseRequestBroadcastReceiver extends ParsePushBroadcastReceiver {
@Override
protected void onPushReceive(Context context, Intent intent) {
int notificationId = 51;
try {
String action = intent.getAction();
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
// replace openAppIntent with event modification
Intent openAppIntent = new Intent(context, MatchesActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MatchesActivity.class);
stackBuilder.addNextIntent(openAppIntent);
PendingIntent acceptIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent declineIntent = null;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_login)
.setContentTitle("Fiternity")
.setContentText(intent.getDataString())
.addAction(android.R.drawable.ic_input_add, "Accept", acceptIntent)
.addAction(android.R.drawable.ic_input_delete, "Decline", declineIntent);
notificationManager.notify(notificationId, notificationBuilder.build());
} catch (JSONException e) {
Log.e("ReceiverIssue", "JSONException: " + e.getMessage());
}
}
}
我有一种预感,它与我在 ParseInstallation 中存储数据的方式有关。我将 facebookId 保存到特定的 ParseInstallation,但我有一个单独的 table,其中包含用户对象。我需要将 ParseUser 对象保存到 ParseInstallation 吗?
非常感谢任何帮助,谢谢!
我放弃了使用 Parse User 或 Parse Installation 作为 ID 的尝试,而是使用 Facebook ID 来获取推送通知。
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("facebookId", recipientId);