I am not getting FCM message probably FirebaseMessagingService is not running


I am not getting FCM message probably FirebaseMessagingService is not running



I am using FCM for notification. Due to some issue, i am not getting notification when i am sending using FCM Cloud console.


<service android:name=".MyInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyMessagingService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>



This is my both classes.


public class MyInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyInstanceIDService";

@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
System.out.println("Refreshed token: " + refreshedToken);
// Util.getInstance().saveDeviceID(UniversalImageLoaderConfigurationApplication.getContext(), refreshedToken);
// sendRegistrationToServer(refreshedToken);
}

}

public class MyMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyMessagingService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
// if(LOG)
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

Map<String, String> data = remoteMessage.getData();
int id = Integer.parseInt(data.get("id"));
if (id == (Constants.NOTI_NOTIFCATION)) {
String title = data.get("title");
String body = data.get("body");
sendNotification(id, title, body, "");
} else {
sendNotification(1, "", "", "");
}

}

}



I noted that public void onTokenRefresh() never called. So i used it



String refreshedToken = FirebaseInstanceId.getInstance().getToken();



in my MainActivity and i am getting tokenID. probably FirebaseInstanceIdService and FirebaseMessagingService is not running. below is my gradle


implementation 'com.google.firebase:firebase-messaging:17.0.0'
implementation 'com.google.firebase:firebase-core:16.0.1'



and i also used


apply plugin: 'com.google.gms.google-services'



this is my root level gradle


buildscript {
repositories {
// google()
maven { url "https://maven.google.com" }
jcenter()
// mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:4.0.1' // google-services plugin

}

}

allprojects {
repositories {
// google()
mavenLocal()
maven { url "https://maven.google.com" }
jcenter()
mavenCentral()
google()
}
}





onTokenRefresh() is called only once when the app is installed first time or when reinstalled. Uninstall the app and put a log in onTokenRefresh() and install it again and check in the logcat.
– Jinesh Malavia
2 days ago





@JineshMalavia yes. but it never called. something issue in FirebaseInstanceIdService and FirebaseMessagingService. i think service is not running. can we track that it is running?
– Sucbe Sys
2 days ago






As you mentioned that you are not receiving notifications the service is not running.
– Jinesh Malavia
2 days ago




1 Answer
1



The actions are reversed for your service declarations. Change to this:


<service android:name=".MyInstanceIDService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".MyMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>



See the example in the documentation.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV