5

Làm cách nào bạn có thể gửi Tin nhắn đám mây của Firebase từ Ứng dụng Google App Engine/Cloud Endpoints?Cách gửi Tin nhắn trên đám mây của Firebase từ Ứng dụng Google App Engine

Android Studio tự động tạo mã sau để gửi Tin nhắn Google Cloud. Mặc dù bạn có thể sử dụng cùng một mã để gửi FCM nhưng bạn không thể đặt "thông báo" hoặc "mức độ ưu tiên" hoặc bất kỳ thứ gì giống như mã FCM mới sử dụng.

Có nhập liệu gradle cho ví dụ này hay ví dụ về cách sử dụng Firebase Cloud Messaging bên trong ứng dụng App Engine để bạn có thể dễ dàng thực hiện những việc như đặt thông báo, thông báo, ưu tiên, v.v.

Đây là những gì Android Studio điểm cuối đám mây tự động tạo cho bạn hiện:

// 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); 
       } 
      } 
     } 
    } 

Trả lời

5

Hiện không có khách hàng căn cứ hỏa lực để gửi tin nhắn từ một máy chủ ứng dụng. Bạn chỉ cần gửi các yêu cầu HTTP thô với các tải trọng JSON từ điểm cuối của bạn bằng cách sử dụng giao thức được ghi thành tài liệu here. server reference hiển thị thông số nào bạn có thể sử dụng, bao gồm mức độ ưu tiên.

+1

Cảm ơn bạn đã trả lời. Tôi đã hy vọng sẽ có một số loại khách hàng cho fcm nhưng có vẻ như tôi sẽ phải viết tất cả bản thân mình. Các tài liệu hướng dẫn là rất tốt mặc dù vì vậy nó không phải là quá khó. – Micro

+0

@MicroR bạn có nhận được bất kỳ giải pháp nào để gửi fcm từ công cụ ứng dụng google –

+0

@Hasanshaikh Chưa. – Micro

Các vấn đề liên quan