【发布时间】:2016-10-13 17:33:30
【问题描述】:
这是我第一次使用 FCM。
我从firebase/quickstart-android 下载了一个示例并安装了 FCM 快速入门。但我无法从日志中获取任何令牌,即使在应用中点击 LOG TOKEN 按钮。
然后我尝试使用 Firebase 控制台发送消息并设置为目标我的应用程序包名称。我收到了消息。
我想知道FCM可以用吗?GCM一切正常。
解决方案:
因为我不是 Android 开发人员,只是一名后端开发人员。所以我需要一些时间来解决它。在我看来,示例应用程序中存在一些错误。
代码:
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.
}
}
在 MainActivity.java 中添加这个。
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
完成上述操作后,你就得到了Logcat中的Token。但最后,我找到了一种方便的方式来获取它。只需使用调试模式安装示例应用程序,第一次安装时就可以获取令牌。
但是不知道为什么安装的时候不能打印日志。可能和手机系统有关。
然后为什么我无法收到通知。 FirebaseMessagingService.onMessageReceived 未调用 sendNotification
【问题讨论】:
-
如果日志被命中,它将在示例应用程序中显示令牌
-
不,示例应用程序说。如果我点击按钮,它只会在 logcat 中显示令牌。但我在 logcat 中一无所获。@ Shubhank
-
取消注释这行
String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); -
已弃用,因此请从此处更新。 stackoverflow.com/q/51123197/7703497
标签: android firebase firebase-cloud-messaging