taki
taki

Reputation: 1

Why FirebaseMessaging getToken() not working?

I made a messaging class extending FirebaseMessagingService and found that onNewToken method was working fine.

But, when I used the sample code for FirebaseMessaging class to get the current Firebase Token, I ran into an error: error: cannot find symbol method getToken().

What am I missing? Any suggestion would be appreciated.

DataManager.java

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;


public class DataManager {


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 {
                                FirebaseMessaging.getInstance().getToken()
                .addOnCompleteListener(new OnCompleteListener<String>() {
                                   @Override
                                   public void onComplete(@NonNull Task<String> task) {
                                      if (!task.isSuccessful()) {
                                         Log.w(tag, "Fetching FCM registration token failed", task.getException());
                                         return;
                                         }

                                      // Get new FCM registration token
                                      registrationId = task.getResult();
                                     }
                                  });
                                    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;
        }
    }
}

Upvotes: 0

Views: 9800

Answers (2)

wilfred lakatoo
wilfred lakatoo

Reputation: 31

I think like handsben above that the problem lies in the dependencies. I have an alternative suggestion that worked for me. This was the error message supplied by android studio

Cannot resolve method 'getToken' in 'FirebaseMessaging'

In the dependencies (in the app-level build.gradle) was this line

implementation 'com.google.firebase:firebase-messaging:20.1.0'

When I clicked on it android studio suggested I change it to

implementation 'com.google.firebase:firebase-messaging:23.0.5'

which I did and the error went away. Hope this could work for you too

Upvotes: 3

handsben
handsben

Reputation: 52

I think it has to do with firebase dependencies versions mismatch. Try to import the BoM for the Firebase platform. By using BoM your app will always use compatible versions of the Firebase Android libraries.

In your app-level build.gradle you can try add the following;

dependencies {
    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:28.3.0')

    // Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-analytics'
}

You can following the instructions here https://firebase.google.com/docs/cloud-messaging/android/client

Cheers!

Upvotes: 1

Related Questions