I am using FCM to provide notifications in my app. Everything worked well, but the FCM TOKEN is not working. Please how do i go about this? may be i have made some errors.
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
User obj=new User();
for(QueryDocumentSnapshot doc:queryDocumentSnapshots)
obj=doc.toObject(User.class);
db.document("User/" firebaseAuth.getCurrentUser().getEmail()).update("fcmToken",SharedPref.getInstance(getApplicationContext()).getToken())
.addOnCompleteListener(new OnCompleteListener<Void>() {```
And
```<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.Falcon.DigitalLibrary.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />```
CodePudding user response:
Use these permissions in gradle file
//Firebase
implementation platform('com.google.firebase:firebase-bom:28.2.1')
implementation 'com.google.firebase:firebase-core'
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.android.gms:play-services-basement:17.6.0'
implementation 'com.google.firebase:firebase-database:19.7.0'
Plese use these code to get Fcm tokem from firebase
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull com.google.android.gms.tasks.Task<String> task) {
try {
String token = task.getResult();
Log.e("DeviceToken = ",token);
}catch (Exception e){
e.printStackTrace();
}
}
});
CodePudding user response:
Step 1 : Add this snippet to your AndroidManifest.xml File.
<service
android:name=".firebase.MyFirebaseMessagingService/*Path of your firebase class*/"
android:exported="true"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
Step 2: In your firebase class (Either you can override onNewToken Method) or go to step 3.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(@NotNull String token) {
super.onNewToken(token);
getSharedPreferences("_", MODE_PRIVATE).edit().putString("FcmId", token).apply();
FirebaseMessaging.getInstance().subscribeToTopic("global");
}
}
Step 3 : Create this function. and call it in onCreate Method.
private void getFirebaseTokenId() {
try {
FirebaseMessaging.getInstance()
.getToken()
.addOnSuccessListener(token -> {
fcmId = token;
})
.addOnFailureListener(e -> {
if (getActivity() != null && !getActivity().isDestroyed() && !getActivity().isFinishing()) {
if (TextUtils.isEmpty(fcmId)) {
fcmId = MyFirebaseMessagingService.getToken(getActivity());
}
}
});
} catch (Exception e) {
e.printStackTrace();
if (getActivity() != null && !getActivity().isDestroyed() && !getActivity().isFinishing()) {
Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
}