我在GCM遇到问题,所以我迁移到FCM,但我仍然遇到与令牌相关的问题。
我启动了更新后的应用,并检查了有300个新用户, 其中200个获得了令牌,100个没有获得任何令牌,这是一个非常关键的问题。
null令牌的任何解决方案?
答案 0 :(得分:7)
每次全新安装时,都会从MyInstanceIDListenerService类onTokenRefresh()方法获取FCM令牌。
或者您也可以使用以下代码获取FCM令牌:
FirebaseInstanceId.getInstance().getToken();
答案 1 :(得分:2)
class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
//Displaying token in logcat
Log.e(TAG, "Refreshed token: " + refreshedToken);
}
private void sendRegistrationToServer(String token) {
//You can implement this method to store the token on your server
//Not required for current project
}
}
**Sometimes tokens are not generated then write this code**
public class ContCreateTokenService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent serviceIntent = new Intent(this, MyFirebaseMessagingService.class);
startService(serviceIntent);
return START_REDELIVER_INTENT;
}
}
public class FetchNewRefreshToken extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public static final String TAG = FetchNewRefreshToken.class.getSimpleName();
public FetchNewRefreshToken() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
// Resets Instance ID and revokes all tokens.
FirebaseInstanceId.getInstance().deleteInstanceId();
// Now manually call onTokenRefresh()
Log.e(TAG, "Getting new token");
FirebaseInstanceId.getInstance().getToken();
} catch (Exception e) {
e.printStackTrace();
}
}
}
同时将其添加到Menifest文件
中 <service android:name=".ContCreateTokenService" />
<service android:name=".FetchNewRefreshToken " />
在启动活动中: -
startService(new Intent(this, FetchNewRefreshToken.class));
startService(new Intent(this, ContCreateTokenService.class));