gpt4 book ai didi

java - App Engine 后端与 Google Cloud Messaging 向 1000 多名用户发送消息

转载 作者:行者123 更新时间:2023-12-02 04:45:23 24 4
gpt4 key购买 nike

我想向所有用户(~15,000)发送一条消息(例如可用更新)。我已经实现了App Engine Backend with Google Cloud Messaging发送消息。

我已经在 2 台设备上进行了测试。双方都收到消息了但正如谷歌文档所述“GCM 支持单条消息最多 1,000 个收件人。”

My question is how to send same message to remaining 14,000 users in my case? Or the code below will take care of it?

下面是发送消息的代码

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiNamespace;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.inject.Named;

import static com.example.shani.myapplication.backend.OfyService.ofy;

/**
* An endpoint to send messages to devices registered with the backend
* <p/>
* For more information, see
* https://developers.google.com/appengine/docs/java/endpoints/
* <p/>
* NOTE: This endpoint does not use any form of authorization or
* authentication! If this app is deployed, anyone can access this endpoint! If
* you'd like to add authentication, take a look at the documentation.
*/
@Api(name = "messaging", version = "v1", namespace = @ApiNamespace(ownerDomain = "backend.myapplication.shani.example.com", ownerName = "backend.myapplication.shani.example.com", packagePath = ""))
public class MessagingEndpoint {
private static final Logger log = Logger.getLogger(MessagingEndpoint.class.getName());

/**
* Api Keys can be obtained from the google cloud console
*/
private static final String API_KEY = System.getProperty("gcm.api.key");

/**
* Send to the first 10 devices (You can modify this to send to any number of devices or a specific device)
*
* @param message The message to send
*/
public void sendMessage(@Named("message") String message) throws IOException {
if (message == null || message.trim().length() == 0) {
log.warning("Not sending message because it is empty");
return;
}
// crop longer messages
if (message.length() > 1000) {
message = message.substring(0, 1000) + "[...]";
}
Sender sender = new Sender(API_KEY);

Message msg = new Message.Builder().addData("message", message).build();

List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(1000).list();
for (RegistrationRecord record : records) {
Result result = sender.send(msg, record.getRegId(), 5);
if (result.getMessageId() != null) {
log.info("Message sent to " + record.getRegId());
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// if the regId changed, we have to update the datastore
log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
record.setRegId(canonicalRegId);
ofy().save().entity(record).now();
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
// if the device is no longer registered with Gcm, remove it from the datastore
ofy().delete().entity(record).now();
} else {
log.warning("Error when sending message : " + error);
}
}
}
}
}

我知道有类似的问题,但我使用的是 Java 语言。我发现后端使用php语言的问题。所以对我没有帮助!

  1. Google Cloud Messaging: Send message to "all" users
  2. Sending Push Notification on multiple devices

有人成功实现过App Engine+Google Cloud Messaging JAVA语言吗?

在下面的代码行中,如果我将 1000 替换为 15,000 会解决我的问题吗?

List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(1000).list();

请尽快提供帮助。非常抱歉我的英语。如果有人需要其他详细信息,欢迎询问。

感谢您的宝贵时间。

最佳答案

一些注意事项,

1) 向可能大量的用户发送通知可能会花费大量时间,请考虑使用 Task Queues将要在 60 秒限制之外“离线”完成的工作排队。

2) 现在至于 GCM 限制,如果您需要所有用户,但 GCM 允许您一次 1000 个,只需将它们分成 1000 个批处理,并为每个批处理单独发送一条消息。

如果您结合这两个建议,您应该拥有一个相当可扩展的流程,您可以在 1 个请求中查询所有用户,拆分该列表,然后排队将消息一次发送给这些用户 1000 个。

关于java - App Engine 后端与 Google Cloud Messaging 向 1000 多名用户发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com