简体   繁体   English

如何从Google App Engine应用发送Firebase Cloud消息

[英]How to send Firebase Cloud Message from Google App Engine App

How can you send a Firebase Cloud Message from a Google App Engine/Cloud Endpoints App? 如何从Google App Engine / Cloud Endpoints App发送Firebase Cloud消息?

Android Studio automatically generates the following code to send a Google Cloud Message. Android Studio自动生成以下代码以发送Google Cloud Message。 While you can use the same code to send a FCM, you can't set the "notification" or "priority" or anything like that which the new FCM uses. 尽管您可以使用相同的代码发送FCM,但是您无法设置“通知”或“优先级”或新FCM使用的类似内容。

Is there a gradle import for this or an example of how to use Firebase Cloud Messaging inside of an App Engine app so you can easily do things like set the message, notification, priority, etc? 是否为此提供了gradle导入,或者是如何在App Engine应用程序内部使用Firebase Cloud Messaging的示例,因此您可以轻松地进行设置消息,通知,优先级等操作?

This is what Android Studio Cloud Endpoints auto generates for you currently: 这是Android Studio Cloud Endpoints当前为您自动生成的:

// Gradle dependency:
compile 'com.google.gcm:gcm-server:1.0.0'

/**
     * 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(10).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);
                }
            }
        }
    }

There's currently no Firebase client for sending messages from an app server. 当前没有用于从应用程序服务器发送消息的Firebase客户端。 You should just send raw HTTP requests with JSON payloads from your endpoint using the protocol documented here . 您应该只使用此处记录的协议从端点发送带有JSON负载的原始HTTP请求。 The server reference shows what parameters you can use, which includes priority. 服务器参考显示了可以使用的参数,包括优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从服务器端(Google App Engine,Cloud Endpoints)向我的客户端发送信息? - How can I send from the server-side (Google App Engine, Cloud Endpoints) the information to my client? Firebase 通过 POST 请求向 Android 应用主题发送云消息 - Google Oauth Playground 访问令牌到期 - Firebase Send Cloud Message to Android App Topics via POST Request - Google Oauth Playground Access Token Expiration 从Google App Engine调用Firebase数据库 - Call Firebase database from Google App Engine 将firebase auth与google app引擎云端点集成 - Integrate firebase auth with google app engine cloud endpoints 如何通过http协议将数据从Android发送到Google App Engine? - How to send data from Android to Google App Engine by http protocol? Firebase 消息:向所有使用云的应用程序用户发送数据消息 function - Firebase Messaging: Send a data message to all app users with a cloud function 从Android应用到Google Cloud Engine应用的HTTP请求身份验证 - Authentication of HTTP request from an Android App to A Google Cloud Engine App 带有Google Cloud Messaging的App Engine - App Engine with Google Cloud Messaging 带有应用引擎的Google云存储 - Google cloud storage with app engine 如何将数据从C ++桌面应用程序发送到Google Firebase? - How to send data from C++ desktop App to Google Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM