5

Google App Engine/Cloud EndpointsアプリケーションからFirebase Cloudメッセージを送信するにはどうすればよいですか?Google App Engine AppからFirebase Cloudメッセージを送信する方法

Androidスタジオは自動的にGoogleクラウドメッセージを送信するために次のコードを生成します。同じコードを使用してFCMを送信することはできますが、新しいFCMが使用するような「通知」や「優先度」などを設定することはできません。

これは、App Engineアプリ内でFirebase Cloud Messagingを使用する方法の例です。メッセージ、通知、優先度などを簡単に設定できますか?

これは、Android Studioのクラウドエンドポイントの自動現在、あなたのために生成するものである:

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

答えて

5

にはFirebaseクライアントは、アプリケーションサーバからメッセージを送信するために現在ありません。 hereに記載されているプロトコルを使用してエンドポイントからJSONペイロードを使用して未処理のHTTPリクエストを送信するだけです。 server referenceには、使用できるパラメータが示されています(優先度を含む)。

+1

お返事ありがとうございます。私はfcmのための何らかのクライアントが既に存在することを望んでいましたが、私はそれをすべて自分で書く必要があるように見えます。ドキュメントは非常に良いですが、それほど難しいものではありません。 – Micro

+0

@MicroR Googleのアプリケーションエンジンからfcmを送信する解決策はありませんでした –

+0

@Hasanshaikhいいえ、まだありません。 – Micro

関連する問題