如何检测令牌是否已过期或未在应用服务器上注册 Firebase FCM 通知?

pcj*_*pcj 6 android push-notification firebase firebase-cloud-messaging firebase-notifications

我正在使用以下代码将 FCM 通知从服务器发送到设备:

    String fcmServerKey = externalConfig.getFcmServerKey();
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(HTTPS_FCM_GOOGLEAPIS_COM_FCM_SEND);

            httpPost.setEntity(new StringEntity(message, ContentType.create("application/json")));
            httpPost.setHeader("Authorization", "key=" + fcmServerKey);

CloseableHttpResponse closeableHttpResponse= httpclient.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,当我获得响应对象时closeableHttpResponse,如何检测用于发送此请求的 fcm 令牌是否已过期或未注册?

从 firebase 应用程序仪表板中时,我尝试使用其 fcm 令牌将通知发送到设备,并且在从设备中删除应用程序后,我Failed在 firebase 仪表板上看到,将光标悬停在Failed我看到的 上Unregistered registration token。

如何检测Unregistered registration token来自 api 响应对象的上述错误情况closeableHttpResponse?

loo*_*p88 5

使用服务器参考 API获取有关设备注册令牌的关联信息。如果响应为空,则表示令牌已过期或未注册。

示例 GET 请求

https://iid.googleapis.com/iid/info/nKctODamlM4:...clJONHoA?details=true
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
Run Code Online (Sandbox Code Playgroud)

结果示例

HTTP 200 OK
  {
    "application":"com.iid.example",
    "authorizedEntity":"123456782354",
    "platform":"Android",
    "attestStatus":"ROOTED",
    "appSigner":"1a2bc3d4e5",
    "connectionType":"WIFI",
    "connectDate":"2015-05-12
    "rel":{
        "topics":{
          "topicname1":{"addDate":"2015-07-30"},
          "topicname2":{"addDate":"2015-07-30"},
          "topicname3":{"addDate":"2015-07-30"},
          "topicname4":{"addDate":"2015-07-30"}
                  }
           }   
  }
Run Code Online (Sandbox Code Playgroud)


Bhu*_*eda 0

在你的包中创建这个类

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.google.gson.Gson;



public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";


    @Override
    public void onTokenRefresh() {

        //Getting registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        //Displaying token on logcat
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);



    }

    private void sendRegistrationToServer(String token) {

        Context context = getApplicationContext(); 
        //You can implement this method to store the token on your server
        //if token is already inserted then update token in your server

    }


}
Run Code Online (Sandbox Code Playgroud)

并将其添加到您AndroidManifest.xml的

<application>
        ..............
        <service
            android:name=".Listeners.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

</application>
Run Code Online (Sandbox Code Playgroud)

  • 嗨,Babul,谢谢,我的问题不是如何检测新令牌,如何检测服务器上过期或未注册的令牌 (3认同)
  • 虽然此代码可以回答问题,但提供有关如何和/或为何解决问题的附加上下文将提高​​答案的长期价值。 (3认同)
  • 正如OP提到的,这个答案与解决方案无关。 (2认同)