quickstart-android: quickstart-android messaging app doesn't receive data messages in background
Step 1: Are you in the right place?
- Issues related to the code in this repository.
Step 2: Describe your environment
- Android device: Asus Nexus 7 wifi
- Android OS version: 6.0.1
- Google Play Services version: 3.0.0
- Firebase/Play Services SDK version: 9.2.1
But I cannot get it working on Nexus 7 LTE 6.0.1 or Samsung GT-9295 5.0.1 as well.
Step 3: Describe the problem:
I send the following message with Advanced REST Client, the message is sent successfully, but is only received in foreground and not when the application is in background.
{
"to" : "egu9jGiMcew:APA91bFv2RewdzlrC4Q0 ... dYZgv91oZVfTtrfAojLMKZZmEURvbq-aA2",
"data": {
"title": "Title test",
"msg": "Text of the test",
"code": 2
},
"delay_while_idle" : false,
"priority" : "high",
"content_available" : true
}
Steps to reproduce:
- Build the quickstart-android project
- Execute the messaging app
- Send it a data message when the application is in background
Observed Results:
-
In my case, the message given above is not received. Is there any mistake in it?
-
It seems that the message is received by the device as I see the following messages:
07-18 15:28:34.371 3068-3068/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.google.firebase.quickstart.fcm (has extras) }
Expected Results:
- The message should be received and generate a notification the application is in foreground or in background.
Relevant Code:
My FirebaseMessagingService is:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
// [END_EXCLUDE]
// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.i(TAG, "From: " + remoteMessage.getFrom());
Log.i(TAG, "Notification Message Body: " + remoteMessage.getData());
sendNotification("FCM message received");
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 79 (13 by maintainers)
@privatejava This might be helpful for you https://stackoverflow.com/a/45810771/1993001
The issue here is a result of some devices prematurely closing applications when they are swiped from the recents view. We are working with device makers to ensure that they are correctly handling dismissal from the recents view. This is not an FCM specific issue, GCM will be affected in the same way.
If your app is in the background you should receive data messages in the onMessageReceived callback and notification messages would result in a displayed notification. If you want to highly customize the notification that is shown then you should use data messages and handle the notification creation on the device side.
Just to add that for me the problem is solved in Android Studio 2.2.2 145.3360264.
FCM data messages are well received in onMessageReceive() when the app is in background and custom notifications are successfully generated !!! 😉
THAT’S COMPLETELY INCREDIBLE
I’ve reisntalled Android Studio 1.5 bundle 141.2422023, sdk 23.0.3, java jdk1.7.0_79 and rebuilt your application.
The result is amazing. I don’t have the Force Stopping message anymore when I put the application in background.
And, it correctly receives and processes the data message in background
messaging app has been successfully rebuilt under Studio 1.5.2 with sdk 24, buildtools 24.0.0, and java 1.8.0_92
Please Please Pleasssssssseeeeeeeee tell me what’s wrong !!!
Hii,
for me tested other devices inlcuding Moto G edition and One Plus 2 it is working and on Mi5 it is not working, was getting same error.
Updated: @kroikie application was not force close just swiped away from recents tray, also if i just press home button(app is minimise) it i am able to see notification in notification tray.
After googling a bit looks like lots of mi5 users are getting this issue from http://en.miui.com/thread-268224-1-1.html and http://en.miui.com/thread-272403-1-1.html , tried that but no luck.
Same issue here:
`05-31 15:33:34.493: W/GCM(3435): Mismatched messenger
05-31 15:34:52.593: W/GCM-DMM(3435): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.whatever (has extras) }
05-31 15:34:52.603: W/GCM-DMM(3435): Unregister application com.whatever for user 0`
I’m using AS 2.3.2, JRE: 1.8.0_112., Firebase 10.2.4, device is Samsung Galaxy J5 (SM-J500M) and Android version is 6.0.1
Hello @miroslavign, Glad to here that, I arrived to the same conclusion where this feature strangely stopped to work with the first 2.X AS versions.
@MiniJay
com.google.firebase.iid.FirebaseInstanceIdReceiver
this serves both the FirebaseMessagingService and the FirebaseInstanceIDService.@lenguyenthanh you are retrieving the data correctly, however be sure to check that the intent extra exists since sometimes the intent may be fired without those extras.
@mindfreak269 what you are seeing is working as intended, if the app is killed by the user the expectation is that it should not be restarted without their explicit indication (by launching the app) that they want the app to run. Thus if your app is killed it should not receive FCM messages. From docs:
@2ndGAB I will take your issue back to the Android Studio team. What platform are you running AS on?
@lenguyenthanh I would prefer to let @kroikie to answer that point as I didn’t explore this feature again from my side.
This error is seen when your application has been forced stopped.
Using the command: adb shell dumpsys package <package-name> | grep stopped
If you get the result: User 0: installed=true hidden=false stopped=false notLaunched=false enabled=0 Then since stopped is false your app should get the notification. If however your result is: User 0: installed=true hidden=false stopped=true notLaunched=false enabled=0 Then since stopped is true your app cannot be started to handle the incoming message.
Could you check if when you remove the app from the recents list or just pause the app by hitting the home button, what the status of your app is? stopped=true or stopped=false?
The expectation is that when you swipe an app from recents the stopped value should still be false, but some launchers are not adhering to this, so if you can confirm the status of your app we can narrow down the issue.