在从 Parse 迁移到 Back4App 之前,我可以通过查询我想要向其发送推送通知的用户的 Installation 对象来轻松发送这样的推送通知:
private void handleLikeNotification(ParseObject messageObject) {
String userId = messageObject.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER).getObjectId();
String result = messageObject.getString(ParseConstants.KEY_NOTIFICATION_TEXT);
// Initiate installation query
ParseQuery<ParseInstallation> pushQuery = ParseInstallation.getQuery();
pushQuery.whereEqualTo("userId", userId);
// Create push notification
ParsePush push = new ParsePush();
push.setQuery(pushQuery);
push.setMessage("Liked by " + ParseUser.getCurrentUser().getUsername() + ": " + result);
push.sendInBackground();
// Check condition and send push notification
if (!userId.equals(ParseUser.getCurrentUser().getObjectId())) {
send(notification);
}
protected void send(ParseObject notification) {
notification.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
// success!
} else {
// notification failed to send!
}
}
});
}
我已遵循他们的文档,并且我的接收器工作正常,因为我可以通过他们的仪表板向自己和我的用户发送通知。
我对 StackOverflow 的问题是为什么我不能通过我的应用创建和发送这样的推送通知?这段相同的代码适用于我的应用程序,该应用程序仍然由原始 Parse 托管解决方案托管。
我的推送通知现在由 GCM 提供服务,因此我想我需要使用 GCM 类发送它们,但我不知道在哪里查看。
此时我最好的假设是 ParsePush
类不适用于我当前的配置。有什么替代方案吗?
最佳答案
刚刚检查了 Back4App 的文档,似乎他们有一些关于如何设置客户端推送的步骤,这似乎是您遇到的问题。
根据它,您需要将 GCM 发件人 ID 放在 Parse 初始化之后,如下所示:
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("GCMSenderId", "Your GCM Sender ID here");
installation.saveInBackground();
之后,只需使用 ParsePush 查询:
ParseQuery pushQuery = ParseInstallation.getQuery();
// Here you can personalize your pushQuery
// Add channels as you like, or remove this part to reach everyone
LinkedList<String> channels = new LinkedList<String>();
channels.add("group_1");
// Change this "message" String to change the message sent
String message = "Back4App says Hi!";
// In case you want to send data, use this
JSONObject data = new JSONObject("{\"rating\": \"5 stars\"}");
ParsePush push = new ParsePush();
// Set our Installation query
push.setQuery(pushQuery);
// Sets the channels
push.setChannels(channels);
// Sets the message
push.setMessage(message);
// Sets the data
push.setData(data);
push.sendInBackground();
文档还说您需要启用客户端推送选项。
也许这应该有助于解决您的问题。
关于java - 使用 Back4App 发送 ParsePush 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39213189/