簡體   English   中英

GCM到FCM-在哪里添加服務器密鑰和處理MessageException

[英]GCM to FCM - Where to add Server Key and handling MessageException

我根據后端不推薦使用的GCM進行遷移。 添加管理SDK

dependencies {
  implementation 'com.google.firebase:firebase-admin:5.11.0'
}

之后,我確實按照以下方式發送消息

String registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setToken(registrationToken)
    .build();

// Send a message to the device corresponding to the provided
String response = FirebaseMessaging.getInstance().sendAsync(message);

這里有兩個問題:

1.我們如何在此處設置密鑰? 在GCM中,我們可以通過new Sender(GCM_key).send(Message)但是我在這里找不到如何設置密鑰的方法。 我不認為setToken方法用於設置密鑰。

2.我們在這里如何處理異常? 庫中有一個名為FirebaseMessageException的類,它具有擴展異常,那么如何處理呢?

任何解決方案將不勝感激。

謝謝

  1. 使用Admin SDK發送FCM消息時,您無需在請求中的任何位置設置服務器密鑰,因為Admin SDK的名稱與Firebase Services 一樣具有類似管理員的特權,因此使用它是自動授權的。

  2. sendAsync()文檔所述:

返回:一個ApiFuture ,將在發送消息后以消息ID字符串完成。

從返回的ApiFuture ,我假設您只是添加一個偵聽器(即addListener() )或使用ApiFutures util類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM