Each time you log in, a new token is generated.
These using the last token generated to send the message ?
My guess is that you might be using an expired token to push the message.
I usually save the last token in Shared preferences and onCreate in the authentication part of firebase auth, write to the Realtime database in a specific node with user data. (if you want to publish the excerpt of these codes for storage of the last token generated).
FCM token (google) associate to User, at which time?
- ====== T U A L I Z A =======
Code snippets I use:
Manifesto:
<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);
}
I, uninstall for example the app and debug by putting the break there, then I take the token and try to send the message. If this is not what is generated for the record, I would like a code snippet yes, but this is the firebase documentation code.
– Henrique
I did not test in debug mode, but in the test I just performed on a published application and another in beta version, using the tokens recorded on firebase Realtime database within the node with user information, it worked perfectly and I received FCM notification instantly. The code I posted as an example keeps the token always up to date in the firebase user database.
– Itapox
The code you use is the same, the problem really is in debug, with the app in release it works. Thanks help!! I’ll check why debug doesn’t work.
– Henrique