我正在按照https://firebase.google.com/docs/cloud-messaging/android/client上的说明在 Android 上设置 Firebase 云消息传递客户端应用程序。我已经编辑了我的应用程序清单以包含 MyFirebaseMessagingService 和 MyFirebaseInstanceIDService 服务。我现在正在尝试访问设备注册令牌。这是我的MyFirebaseInstanceIDService.java
文件的内容(从https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseInstanceIDService获得的代码.java):
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.[myapp];
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
System.out.println("Registration.onTokenRefresh TOKEN: " + refreshedToken );
}
// [END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
由于我正在使用Log.d(TAG, "Refreshed token: " + refreshedToken);
,我希望在 Android Studio 中从 Android Monitor 中看到令牌,但我看不到它。你知道我需要做什么onTokenRefresh()
才能调用该方法以便FirebaseInstanceId.getInstance().getToken()
执行并生成令牌吗?谢谢你。