我想在制作Android应用程序时,在同一个包中的不同类(例如DataManager.java(中使用一个新的Firebase令牌。但当我运行DataManager.java.时,它总是无法获取令牌字符串
除了将字符串存储到服务器数据库或SharedPreference之外,还有其他方法可以获取字符串吗?任何建议都将不胜感激。
DataManager.java
public class DataManager {
public static GCMBroadcastReceiver _fcm = null;
public static String registerGoogleServiceInBackground( final OnDataManagerRegisterGooglePlayServiceListener listener )
{
boolean isEnabledPlayService = true;
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable( getContext() );
{
if( GooglePlayServicesUtil.isUserRecoverableError( resultCode ) )
{
GooglePlayServicesUtil.getErrorDialog( resultCode, (Activity) getContext(), PLAY_SERVICES_RESOLUTION_REQUEST ).show();
} else {
Log.i( "MainActivity.java|checkPlayService", "|This device is not supported.|" );
}
isEnabledPlayService = false;
}
if( isEnabledPlayService )
{
if( TextUtils.isEmpty( registrationId ) )
{
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground( Void... params )
{
String msg;
try {
if( _fcm == null ){
_fcm = new GCMBroadcastReceiver();
Log.d("TEST_FCM", _fcm.getRegId());
}
_fcm.onNewToken(GCMBroadcastReceiver.token);
registrationId = _fcm.getRegId();
msg = "Device registered, registration ID=" + registrationId;
Log.d("Device registered", "|" + registrationId);
} catch( Exception ex ) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute( String msg ){
if( listener != null ) listener.onFinish( true, registrationId );
Log.i( "MainActivity.java | onPostExecute", "|" + msg + "|" );
}
}.execute( null, null, null );
return "";
}
} else {
if( listener != null ) listener.onFinish( true, registrationId );
return registrationId;
}
} else {
Log.i( "MainActivity.java | onCreate", "|No valid Google Play Services APK found.|" );
if( listener != null ) listener.onFinish( true, registrationId );
return null;
}
}
}
GCMBroadcastReceiver.java
public class GCMBroadcastReceiver extends FirebaseMessagingService
{
public static String token = null;
private static String registrationId = "123";
@Override
public void onNewToken(String token)
{
super.onNewToken(token);
Log.d("FCM_token", token);
registrationId = token;
}
public String getRegId() {
return registrationId;
}
}
日志(已编辑(
2021-05-24 16:54:38.013 1767-1866/com.counsring D/TEST_FCM: 123
2021-05-24 16:54:38.652 1767-1767/com.counsring I/MainActivity.java | onPostExecute: |Error :println needs a message|
2021-05-24 16:54:39.869 1767-1923/com.counsring D/FCM_token: foyci6cQA-M:APA91bHafND9btbiSyI9M6UhptfQxe1T9NZM72fKc4ZMhHFPS2bDjdlsWjA99vYkwt3zr3TJZYY4pF1rrfkmpl7hHmZgDn1oKnxC9fJodfwYbXdq2Z3I5XDzwpByjw6F
2021-05-24 16:54:39.869 1767-1923/com.counsring D/RegIdTest: foyci6cQA-M:APA91bHafND9btbiSyI9M6UhptfQxe1T9NZM72fKc4ZMhHFPS2bDjdlsWjA99vYkwt3zr3TJZYY4pF1rrfkmpl7hHmZgDn1oKnxC9fJodfwYbXdq2Z3I5XDzwpByjw6F
检索当前令牌FirebaseMessaging.getInstance().getToken();
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
String token = task.getResult();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
处理新令牌(在您的信息类中(
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}
欲了解更多信息,请查看Firebase Docs