FirebaseInstanceId.getInstance().getToken() in MyFirebaseInstanceIDService.java 未出现在日志中
FirebaseInstanceId.getInstance().getToken() in MyFirebaseInstanceIDService.java not appearning in the log
我正在按照 https://firebase.google.com/docs/cloud-messaging/android/client to set up Firebase Cloud Messaging Client App on Android. I already edited my app's manifest to include the MyFirebaseMessagingService and MyFirebaseInstanceIDService services. I am trying to access the device registration token now. This is the content of my MyFirebaseInstanceIDService.java
file (code obtained from 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 监视器的令牌,但我看不到它。您知道我需要做什么才能调用 onTokenRefresh()
方法,以便执行 FirebaseInstanceId.getInstance().getToken()
并生成令牌吗?谢谢。
我只需要从我的应用程序的第一个 activity 开始就使用它:
@Override
protected void onCreate(Bundle savedInstanceState) {
// Get token
String token = FirebaseInstanceId.getInstance().getToken();
System.out.println("Token obtained from [MyFirstActivity].java: "+token+" Token received successfully.");
}
在 Android 监视器中,我可以看到类似这样的内容:
11-27 11:08:49.213 10730-10730/com.[myapp] I/System.out: Token obtained from [MyFirstActivity].java: f95EVa9yfbo:APA91bFm56ZuQLWYSDDgPlkf4a88Lu6Gp4DoXVDJ5dRIlnjDncq0UdNnlSi7wxbbut6YX7Z1kmvyS3bzk_Zrl-1doHCw5XFeOXTthNzo4sXASWqQjHdfuA3pH3Js4wlbf_CnRkw5Mzao Token received successfully.
您是否在清单中注册了该服务?这通常会在使用 android studio 时自动发生,但请确保它如 reference
中所述
onTokenRefresh
仅在令牌更新时调用。您应该使用 FirebaseInstanceId.getInstance().getInstanceId()
请求它。 FirebaseInstanceId.getInstance().getToken()
已弃用并阻塞 - 因此,从 onCreate
调用它是一个非常糟糕的主意,因为它会导致挂在 main/ui 线程
上
我正在按照 https://firebase.google.com/docs/cloud-messaging/android/client to set up Firebase Cloud Messaging Client App on Android. I already edited my app's manifest to include the MyFirebaseMessagingService and MyFirebaseInstanceIDService services. I am trying to access the device registration token now. This is the content of my MyFirebaseInstanceIDService.java
file (code obtained from 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 监视器的令牌,但我看不到它。您知道我需要做什么才能调用 onTokenRefresh()
方法,以便执行 FirebaseInstanceId.getInstance().getToken()
并生成令牌吗?谢谢。
我只需要从我的应用程序的第一个 activity 开始就使用它:
@Override
protected void onCreate(Bundle savedInstanceState) {
// Get token
String token = FirebaseInstanceId.getInstance().getToken();
System.out.println("Token obtained from [MyFirstActivity].java: "+token+" Token received successfully.");
}
在 Android 监视器中,我可以看到类似这样的内容:
11-27 11:08:49.213 10730-10730/com.[myapp] I/System.out: Token obtained from [MyFirstActivity].java: f95EVa9yfbo:APA91bFm56ZuQLWYSDDgPlkf4a88Lu6Gp4DoXVDJ5dRIlnjDncq0UdNnlSi7wxbbut6YX7Z1kmvyS3bzk_Zrl-1doHCw5XFeOXTthNzo4sXASWqQjHdfuA3pH3Js4wlbf_CnRkw5Mzao Token received successfully.
您是否在清单中注册了该服务?这通常会在使用 android studio 时自动发生,但请确保它如 reference
中所述onTokenRefresh
仅在令牌更新时调用。您应该使用 FirebaseInstanceId.getInstance().getInstanceId()
请求它。 FirebaseInstanceId.getInstance().getToken()
已弃用并阻塞 - 因此,从 onCreate
调用它是一个非常糟糕的主意,因为它会导致挂在 main/ui 线程