I am working on an android project for which I am using Firebase cloud messaging to send some push notifications. Messages used to deliver correctly. I wanted to update the app with the latest firebase API. After doing that, I don't know what happened now cloud messages are not delivering at all. when app opened user gets subscribed to a topic called "update". like below:
FirebaseMessaging.getInstance().subscribeToTopic("update")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Log.d("SUBSCRIPTION","FAILURE");
Toast.makeText(getApplicationContext(),"FAILURE",Toast.LENGTH_SHORT).show();
}else{
Log.d("SUBSCRIPTION","SUCCESS");
Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show();
}
}
});
I have written this code in the First activity's onCreate method. I have also observed that the addOnCompletelistener in the above code is not at all getting called.
Android side code:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("FROM", "From: " + remoteMessage.getFrom());
Log.d("DATA", "Message data payload: " + remoteMessage.getData());
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("TAG", "Refreshed token: " + s);
}
}
Manifest file: I have added in the application tag of manifest file.
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Gradle App :
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-crash:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
}
apply plugin: 'com.google.gms.google-services'
Gradle project:
buildscript {
repositories {
jcenter()
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:4.0.1'
}
}
I am using postman to send push notifications:
https://fcm.googleapis.com/fcm/send
Authorization: key=xxxxxxxxxxxxx // server key
METHOD: POST
{
"to":"/topics/xxxx",
"collapse_key" : "xxxxxxxx",
"data" : {
"versioncode":107
}
}