Firebase(FCM)如何获取令牌 [英] Firebase (FCM) how to get token
问题描述
代码:
RegistrationIntentService.java
public class RegistrationIntentService extends Inten tService {
private static final String TAG =RegIntentService;
public RegistrationIntentService(){
super(TAG);
$ b @Override
保护无效的onHandleIntent(意图意图){
字符串标记= FirebaseInstanceId.getInstance()。getToken();
Log.i(TAG,FCM Registration Token:+ token);
$ b MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG =MyFirebaseIIDService;
/ **
*如果InstanceID令牌更新,则调用。如果
*之前的标记的安全性已经被破坏,则可能发生这种情况。请注意,当最初生成InstanceID令牌
*时会调用它,所以这就是您要检索令牌的地方。
* /
// [START refresh_token]
@Override $ b $ public void onTokenRefresh(){
//获取更新的InstanceID标记。
// String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
// Log.d(TAG,Refreshed token:+ refreshedToken);
//
// // TODO:实现此方法将任何注册发送到您的应用程序的服务器。
// sendRegistrationToServer(refreshedToken);
//
Intent intent = new Intent(this,RegistrationIntentService.class);
startService(intent);
}
// [END refresh_token]
/ **
*将令牌保留给第三方服务器。
*< p>
*修改此方法以将用户的FCM InstanceID令牌与您的应用程序维护的任何服务器端帐户
*相关联。
*
* @param令牌新的令牌。
* /
private void sendRegistrationToServer(String token){
//根据需要添加自定义实现。
$ b $在MainActivity.java中添加这个。 Intent intent = new Intent(this,RegistrationIntentService.class);
startService(intent);
完成上述操作后,您可以在logcat中获得Token。但最后我发现一个方便的仅使用调试模式安装示例应用程序,您可以在第一次安装示例应用程序时获取令牌。
但是我不知道为什么它可以在安装时打印日志。可能与移动系统有关。
然后为什么我不能得到通知。 FirebaseMessagingService.onMessageReceived 没有调用 sendNotification
解决方案 / p>
Log.d(Firebase,token+ FirebaseInstanceId.getInstance()。getToken());
It`s my first to use FCM.
I download a sample from firebase/quickstart-android and I install the FCM Quickstart.But I can`t get any token from the log even hit the LOG TOKEN button in the app.
Then I try to send message with Firebase console and set to target to my app package name.I got any incoming message.
I want to know can FCM be used?GCM everything is ok.
Solution:
Because I am not a Android developer,just a backend developer.So it takes me some time to solve it.In my opinion,there`re some bug in the sample app.
Code:
RegistrationIntentService.java
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public RegistrationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "FCM Registration Token: " + token);
}
}
MyFirebaseInstanceIDService.java
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);
//
// // TODO: Implement this method to send any registration to your app's servers.
// sendRegistrationToServer(refreshedToken);
//
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
// [END refresh_token]
/**
* Persist token to third-party servers.
* <p>
* 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) {
// Add custom implementation, as needed.
}
}
Add this in the MainActivity.java.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
After do above,you can get the Token in logcat.But finally I find a convenient way to get it.Just use debug mode to install the sample app and you can get the token when you first time to install it.
But I dont why it can
t print the log when I install it.Maybe be related to mobile system.
And then why I can`t get the Notification. FirebaseMessagingService.onMessageReceived did not call sendNotification
解决方案 Work for me
Log.d("Firebase", "token "+ FirebaseInstanceId.getInstance().getToken());
这篇关于Firebase(FCM)如何获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!