Issue
I've implemented FCM on my server with an Android client, and it worked fine for a while. All of a sudden, the client stopped receiving notifications for 5-10 minutes at a time, after which it would get all the pending notifications at once. The logs show that the server is sending the messages correctly. When the message queue gets stuck, not even test messages from the Firebase console get through.
The only relevant thing I've found in the official documentation was regarding the lifetime of a message:
If the device is not connected to FCM, the message is stored until a connection is established (again respecting the collapse key rules). When a connection is established, FCM delivers all pending messages to the device.
But my network is working fine, and this issue has arisen on other networks and clients as well. When I freshly install the app, a new token is being generated and successfully received by my server. It's just the server-to-client messages that aren't going through.
How can I determine if the connection is established and fix it? Or what else could be the cause of this?
This is my client-side code
AndroidManifest.xml
<application
android:name=".App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<service
android:name=".service.NotificationService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
NotificationService.java
public class NotificationService extends FirebaseMessagingService {
private static final String TAG = "NotificationService";
private static String token;
public NotificationService() {
token = PreferenceManager
.getDefaultSharedPreferences(App.getContext())
.getString("firebase-token", null);
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
token = task.getResult().getToken();
Log.d(TAG, "onComplete: token = " + token);
PreferenceManager
.getDefaultSharedPreferences(App.getContext())
.edit()
.putString("firebase-token", token)
.apply();
}
});
}
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "Received message of size " + remoteMessage.getData().size());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
public void run() {
int left = -1, right = -1;
if (remoteMessage.getData().get("left") != null) {
left = Integer.parseInt(Objects.requireNonNull(remoteMessage.getData()
.get("left")));
}
if (remoteMessage.getData().get("right") != null) {
right = Integer.parseInt(Objects.requireNonNull(remoteMessage.getData()
.get("right")));
}
if (remoteMessage.getData().get("REFRESH") != null) {
Util.sendTitleBroadcast(getApplicationContext(),
left, right,
Util.REFRESH_TITLE);
} else {
Util.sendTitleBroadcast(getApplicationContext(),
left, right,
Util.TITLE_DATA_RECEIVED);
}
}
});
}
}
public static String getToken() {
if (token == null) {
token = PreferenceManager
.getDefaultSharedPreferences(App.getContext())
.getString("firebase-token", null);
}
return token;
}
}
Code for server-side FirebaseMessagingService.java
public class FirebaseMessagingService {
private static final Logger logger = Logger
.getLogger(FirebaseMessagingService.class);
private static FirebaseMessagingService instance;
/**
* Path to resource file that contains the credentials for Admin SDK.
*/
private final String keyPath = "firebase-adminsdk.json";
/**
* Maps a token list to each topic Object.
*/
private static Map<Object, List<String>> topicTokenMap;
public synchronized static FirebaseMessagingService getInstance() {
if (instance == null) {
instance = new FirebaseMessagingService();
}
return instance;
}
private FirebaseMessagingService() {
init();
}
/**
* Initializes the Firebase service account using the credentials from the
* json file found at keyPath.
*/
private void init() {
try {
InputStream serviceAccount
= getClass().getClassLoader().getResourceAsStream(keyPath);
if (serviceAccount != null) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials
.fromStream(serviceAccount))
.setDatabaseUrl(databaseUrl)
.build();
FirebaseApp.initializeApp(options);
logger.debug("FirebaseMessagingService: init successful");
} else {
logger.debug("FirebaseMessagingService: Input stream null from"
+ " path: " + keyPath);
}
} catch (IOException ex) {
logger.debug("FirebaseMessagingService: Failed to get credentials "
+ "from inputStream." + ex.getMessage());
}
}
/**
* Sends the messages given as parameters to the list of tokens mapped to
* the given topic Object.
*
* @param topic
* @param messages
*/
public void sendMessage(Object topic,
Map<String, String> messages) {
logger.debug("FirebaseMessagingService: sending Message "
+ messages.toString());
try {
if (topicTokenMap != null) {
List<String> tokenList = topicTokenMap.get(topic);
if (tokenList != null) {
tokenList.removeAll(Collections.singleton(null));
MulticastMessage message = MulticastMessage.builder()
.putAllData(messages)
.addAllTokens(tokenList)
.build();
FirebaseMessaging.getInstance().sendMulticast(message);
logger.debug("FirebaseMessagingService: message sent successfully");
}
}
} catch (FirebaseMessagingException ex) {
logger.debug(ex.getMessage());
}
}
/**
* Registers the token given as a parameter to the topic Object.
*
* @param topic
* @param token
*/
public void registerTokenToTopic(Object topic, String token) {
if (topicTokenMap == null) {
topicTokenMap = new HashMap<Object, List<String>>();
}
List<String> tokens = topicTokenMap.get(topic);
if (tokens == null) {
tokens = new ArrayList<String>();
}
if (!tokens.contains(token)) {
tokens.add(token);
}
topicTokenMap.put(topic, tokens);
}
/**
* Unregisters all instances of the token given as a parameter from the topic
* given.
*
* @param topic
* @param token
*/
public void unregisterTokenFromTopic(Object topic, String token) {
if (topicTokenMap != null) {
List<String> tokens = topicTokenMap.get(topic);
if (tokens != null) {
tokens.removeAll(Collections.singleton(token));
}
}
}
/**
* Looks for a topic that has a field with the name equal to topicFieldName
* and the value equal to topicFieldValue and sends a notification to the
* tokens subscribed to that topic, telling them to ask for an update.
*
* @param topicFieldName
* @param topicFieldValue
*/
public void notifyChangeForTopicField(String topicFieldName,
Object topicFieldValue) {
logger.debug("FirebaseMessagingService: notifyChangeForTopicField: "
+ "topicFieldValue = " + topicFieldValue.toString());
Map<String, String> messageMap = new HashMap<String, String>();
messageMap.put("REFRESH", "true");
if (topicTokenMap != null
&& topicTokenMap.entrySet() != null
&& topicTokenMap.entrySet().size() > 0) {
Iterator it = topicTokenMap.entrySet().iterator();
while (it.hasNext()) {
try {
Map.Entry pair = (Map.Entry) it.next();
Object topic = pair.getKey();
logger.debug("FirebaseMessagingService: "
+ "notifyChangeForTopicField topic = " + topic.toString());
Field field = topic.getClass().getDeclaredField(topicFieldName);
logger.debug("FirebaseMessagingService: "
+ "notifyChangeForTopicField field = " + field.toString());
field.setAccessible(true);
logger.debug("FirebaseMessagingService: "
+ "notifyChangeForTopicField field contains topic: "
+ (field.get(topic) != null ? "true" : "false"));
if (field.get(topic) != null
&& field.get(topic).equals(topicFieldValue)) {
sendMessage(topic, messageMap);
break;
}
it.remove();
} catch (NoSuchFieldException ex) {
java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FirebaseMessagingService.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
EDIT: after a few more tests I've found that when one client stops working, the others receive the messages just fine
Solution
I've found the answer here: https://stackoverflow.com/a/23902751/10702209 What happened was that I was getting router timeouts after 5 minutes, randomly. I've fixed the issue by manually sending out heartbeats to FCM after 4 minutes, instead of the default 15 minutes.
context.sendBroadcast(new Intent("com.google.android.intent.action.GTALK_HEARTBEAT"));
context.sendBroadcast(new Intent("com.google.android.intent.action.MCS_HEARTBEAT"));
I've implemented this fix a couple of months ago and haven't had the issue come up again since.
Answered By - Monica Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.