Unregistered registration token - Firebase

0

I have a project in the firebase and I'm trying to implement the push notifications, I followed the documentation but when doing tests, sending direct in the firebase, it fails and the error appears, "Unregistered Registry Token".

Manifest:

<serviceandroid:name=".util.appFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

<service android:name=".util.appFirebaseRegistrationIntentService" android:exported="true"> <!-- tentei com e sem esta tag-->
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

Classes:

public class appFirebaseRegistrationIntentService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d("Token da App", token);
    }
}

public class appFirebaseMessagingService extends FirebaseMessagingService {
     //todo
}

I can not find my error. I get the token that is in the token variable, I put in the message choosing the single device (in the firebase console), but still, I have the error.

    
asked by anonymous 03.09.2017 / 15:38

1 answer

1

Each time you log in, a new token is generated. Are you using the last generated token to send the message?

My guess is that you may be using an expired token to send the push message.

I usually write the last token in the shared preferences and onCreate in the auth part of the firebase auth, in the realtime database in a specific node with the user data (if you want to publish the excerpt of these codes for last generation token storage ).

Token FCM (google) to associate with the User, at what time?

  • ====== A T U A L A N E =======

Code snippets that I use:

Manifest:

    <service android:name=".Messaging.MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service
        android:name=".Messaging.FirebaseIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

.Messaging.FirebaseIDService:

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
        SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = SP.edit();
        editor.putString("CfgTokenFCM", token);
        editor.apply();
    }
}

.Messaging.MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

.Models.User:

// [START blog_user_class]
@IgnoreExtraProperties
public class User {

    public String username;
    public String email;
    public String photo;
    public String token;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String username, String email, String photo, String token) {
        this.username = username;
        this.email = email;
        this.photo = photo;
        this.token = token;
    }

    // [START user_to_map]
    @Exclude
    public Map<String, Object> toMap() {

        HashMap<String, Object> result = new HashMap<>();

        result.put("username", username);
        result.put("email", email);
        result.put("photo", photo);
        result.put("token", token);

        return result;
    }
    // [END posto_to_map]

}
// [END blog_user_class]

MainActivity (onCreate):

    // Initialize Firebase Auth
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    if (mFirebaseUser == null) {
        // Not signed in, launch the Sign In activity
        startActivity(new Intent(this, SignInActivity.class));
        finish();
        return;
    } else {
        mPhotoUrl = "";
        mUserName = mFirebaseUser.getDisplayName();
        mUserID = mFirebaseUser.getUid();
        mUserEmail = mFirebaseUser.getEmail();
        if (mFirebaseUser.getPhotoUrl() != null) {
            mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
        }

        // Read CfgTokenFCM
        SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        cfgTokenFCM = SP.getString("CfgTokenFCM", "");

        // Write user data in SharedPreferences
        SharedPreferences.Editor editor = SP.edit();
        editor.putString("CfgUserID", mUserID);
        editor.apply();

        // Write user data in firebase
        User user = new User( mUserName , mUserEmail, mPhotoUrl, cfgTokenFCM);
        Map<String, Object> userValues = user.toMap();
        Map<String, Object> childUpdates = new HashMap<>();
        childUpdates.put("/users/" + mUserID, userValues);
        FirebaseDatabase.getInstance().getReference().updateChildren(childUpdates);
    }
    
04.09.2017 / 01:31