Fcm Data Messages Are Not Working Properly
Solution 1:
First check this remote message contains payload, May be payload get corrupt for any reason.
publicclassMyFirebaseMessagingServiceextendsFirebaseMessagingService { privatestaticfinalStringTAG="REMOTE_MSG"; @OverridepublicvoidonMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage == null) return; // check if message contains a notification payload.if (remoteMessage.getNotification() != null) { Log.e(TAG, "Notification body: " + remoteMessage.getNotification().getBody()); createNotification(remoteMessage.getNotification()); } // check if message contains a data payloadif (remoteMessage.getData().size() > 0) { Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } }
DON'T forgent to update your FCM device token on your DB.
publicclassMyFirebaseInstanceIdServiceextendsFirebaseInstanceIdService { privatestatic final StringTAG = "FCM_ID"; @OverridepublicvoidonTokenRefresh() { // get hold of the registration tokenString refreshedToken = FirebaseInstanceId.getInstance().getToken(); // lg the tokenLog.d(TAG, "Refreshed token: " + refreshedToken); sendRegistrationToServer(refreshedToken); } privatevoidsendRegistrationToServer(String token) { // implement this method if you want to store the token on your server } }
Update 1
- As mentioned Firebase repo issue expected that issue to be related to device, Please try another device aspect.
Update 2Quote from firebase repo contributorkroikie
FCM does not process messages if an app is "killed" or force stopped. When a user kills an app it is an indication that the user does not want the app running so that app should not run till the user explicitly starts it again.
Note that swiping an app from recents list should NOT "kill" or force stop it. If messages are not being received after swiping from recents list then please identify these devices and we will work with the manufactures to correct this behaviour.
Only being able to handle messages when the app is in the foreground or background is working as intended.
Update 3
Try hacks mentioned on this issue.
Inserting this lines in Manifest.xml
<uses-permissionandroid:name="com.google.android.c2dm.permission.RECEIVE" /><uses-permissionandroid:name="android.permission.WAKE_LOCK" />
It is because of DOZE mode and battery optimization,you just have to turn off battery optimization for all apps or particular app. Go to Settings>> apps >> select your app>> battery>> battery optimization> select your app>> select don't optimise. Problem solved.
Now (for APP IS CLOSED case) I write Notification text to file and read this text if extras == null and notificationText.txt is exists... its stupid solution but it works. How can I catch this extras when app is closed in other way.
Update 4
- Try to Setting the priority of a message,
- Disable Battery Optimization
Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.
An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent to take the user directly to the Battery Optimization, where they can add the app. An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog. The user can manually remove apps from the whitelist as needed.
- Programmatically open Battery Optimization, check this answer.
TAKE CARE ABOUT THIS
Before asking the user to add your app to the whitelist, make sure the app matches the acceptable use cases for whitelisting.
Note: Google Play policies prohibit apps from requesting direct exemption from Power Management features in Android 6.0+ (Doze and App Standby) unless the core function of the app is adversely affected.
Update 5
Check PowerManager.isIgnoringBatteryOptimizations() from this answer.
When disable battery optimization, Consider to check first if it's already disabled then no need to show dialog, else you can show dialog.
/** * return false if in settings "Not optimized" and true if "Optimizing battery use" */privatebooleancheckBatteryOptimized() { finalPowerManagerpwrm= (PowerManager) getSystemService(Context.POWER_SERVICE); try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return !pwrm.isIgnoringBatteryOptimizations(getBaseContext().getPackageName()); } }catch (Exception ignored){} returnfalse; } privatevoidstartBatteryOptimizeDialog(){ try { Intentintent=newIntent(android.provider.Settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:PUT_YOUR_PACKAGE_NAME_HERE")); startActivity(intent); } catch (ActivityNotFoundException e) { e.printStackTrace(); } }
Post a Comment for "Fcm Data Messages Are Not Working Properly"