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?
使用服务器参考 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)
在你的包中创建这个类
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)