I cannot receive any FCM message anymore in backgr

2019-04-12 04:39发布

问题:

I cannot receive any data message anymore in my application when it's in background. Notice that it worked perfectly in my application until very recently. Maybe it stopped working after recent updates in my development environment, I can't say....

So I tried to make the test with the quickstart-android project, to see it works better.

And the answer is no!

I have no doubt that quickstart-android project is well configured to use FCM capabilities...

I send the following message from Advanced REST Client, I used it successfully to test my application currently published.

{
  "to" : "dLN4paNl3uw:APA91bH0lpkEMxGp...._7EdxkB2cRbPKFxT2Ti3OPw-7fCSwXCSfGG",
  "data": {
    "id": 19,
    "title": "Title test",
    "msg": "Text of the test",
    "code": 2,
  },
  "delay_while_idle" : false,
  "priority" : "high",
  "content_available" : true
}

What I can say is that when and every time I send a message while the application is in background, I get the following display in logcat

07-22 12:54:21.400 14316-14316/? 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) }

So the message is well received by the device but not transferred to onMessageReceive().

I saw many posts about such problem but the answers are always vague as what we can say is that it work for some and don't work for some others. So I'd like to know if someone recently found a more precise explanation to this problem because I wonder if it couldn't come from the development environment as I tried to build older working project with the newly updated studio, sdk, build tools and I face the same problem, GCM/FCM messages are not received anymore by the newly built application.

EDIT

After more investigations, I saw in logcat that my application is force closed when I swipe it out from the recent apps list. This certainly explain it cannot be able to handle the message received by the device.

07-23 23:12:29.002 671-31254/? I/ActivityManager: Killing 17453:com.google.firebase.quickstart.fcm/u0a297 (adj 9): remove task
07-23 23:12:29.016 14316-14524/? D/GCM: Removing messenger 0:com.google.firebase.quickstart.fcm
07-23 23:12:29.017 671-2998/? I/WindowState: WIN DEATH: Window{2a27cf7 u0 com.google.firebase.quickstart.fcm/com.google.firebase.quickstart.fcm.MainActivity}
07-23 23:12:30.148 671-4851/? I/ActivityManager: Force stopping com.google.firebase.quickstart.fcm appid=10297 user=0: from pid 18289

I also tried a working project of mine built few weeks ago, based on GCM 8.4.0, sdk 23, java 1.7, which was working and I get the same problem; Application is force closed.

This is observed on 2 different devices I successfully used to test my working project: nexus 7 LTE 6.0.1 and GT-9295 5.0.1
The only difference I see now is the version of Android Studio/gradle. Working project were built with 1.5.2 and now I use 2.1.2, I cannot build any working application anymore.
I get this problem on 2 different computers and on one of them I reinstalled everything!

回答1:

FCM have two type of notification Silent and Non-Silent:-

Silent notification :- You always get callback in onMessageReceived method

Non-Silent Notification :- When app is in foreground you got a callback in onMessageReceived method but when app is in background FCM generate a default notification in notification bar.

A notification with only data part is treated as silent notification in FCM.

When you generate notification from server you pass a JSON object something like that :-

{
  "notification":{
    "title":"Test Notification",
    "body":"Notification is delivered!",
    "sound":"call",
    "icon": "ic_app_icon"
  },
  "data":{
    "someData":"Great"
  },
  "to": "app_token",
  "priority":"high"
}

The above one is a Non-Silent notification if you want to send silent notification from server then the structure should be something like that :-

{
  "data":{
    "someData":"Great"
  },
  "to": "app_token",
  "priority":"high"
}

As you can it is the same structure just without notification object in payload.



回答2:

That's completely incredible

I've reinstalled Android Studio 1.5 bundle 141.2422023, sdk 23.0.3, java jdk1.7.0_79 and rebuilt Firebase quickstart-android messaging application and the result is amazing

I don't have the Force stopping message anymore when I put the application in background:

07-24 12:00:10.409 671-3321/? I/ActivityManager: Killing 11067:com.google.firebase.quickstart.fcm/u0a297 (adj 9): remove task
07-24 12:00:10.425 671-31254/? D/GraphicsStats: Buffer count: 4
07-24 12:00:10.425 671-31254/? I/WindowState: WIN DEATH: Window{996cbf7 u0 com.google.firebase.quickstart.fcm/com.google.firebase.quickstart.fcm.MainActivity}

And the application is correctly receiving and processing data payload message

I also successfully rebuilt the application under Studio 1.5.2 with sdk 24, buildtools 24.0.0, and java 1.8.0_92

Please tell me if there is something critical to change in an application when migrating from Studio 1.5 to Studio 2.1.2 !!



回答3:

I'm also seeing this error, also using Android Studio 2.1, and found out this:

When you quit Android Studio, it will terminate the app (being debugged) and put it into some sort of "stopped" state.

This is not same as swiping off from recents, if I do just that, then my GCM messages get through fine.

But this cycle -- debug with AS, quit AS, app gets killed -- causes GCM messaging to fail with result=CANCELLED forIntent



回答4:

I hope you make this type of data add in your web API.

$fields = array(
    'to'                        => $reg_id ,
    'priority'                  => "high",
    'notification'              => array( "title"=>"Food Shop", "body" => $message , "click_action"=> "OPEN_ACTIVITY_1"),
    'data' => array(
            "noti_title"=>"Food Shop One",
            "noti_body"=> "some body"
          ),
);