Firebase onMessageReceived not called when app is

2019-02-27 03:54发布

I am using Firebase Cloud Messaging on Android.

  • I can successfully receive notification and data messages when the app is in the foreground.
  • The documentation states that if a notification message is received while the app is in the background then Firebase will send a notification to the system tray on behalf of the app. This is not working for me.
  • If a data message is sent and the app is in the background then the onMessageReceived method is called successfully.
  • The most troubling scenario is when I force stop the application and then try sending a notification or data message. The onMessageReceived method doesn't get called in this situation.
  • I have seen a number of articles on SO discussing the background use case, but I have yet to see any explanation for why no messages are being received when the app is stopped entirely.

I have the following in my manifest:

<service android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name=".MyInstanceIdService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

This is what the documentation has to say about extending FirebaseMessagingService:

This is required if you want to do any message handling beyond receiving notifications on apps in the background.

It's not entirely clear what they mean by "message handling beyond receiving notifications on apps in the background". I can only assume that they are talking about handling messages when the app is closed.

Any help would be greatly appreciated.

5条回答
Melony?
2楼-- · 2019-02-27 04:21

The problem is in your backend. See this answer. When the message contains notification object, onMessageReceived is called only when the app is foreground. So you have to use only data object. Eg:

var message = {
         to: user.deviceId, // required
         collapse_key: 'key',
         data: {
              title: 'Hello',
              body: 'Body'
         }
}; 

This works for every scenario.

查看更多
beautiful°
3楼-- · 2019-02-27 04:24

Add action in your manifest activity like below:

<activity
   android:name=".ActivityFCM"
   android:screenOrientation="sensor"
   android:windowSoftInputMode="stateHidden">
   <intent-filter>
       <action android:name="AnyNameYouWant" />
       <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</activity>

And add click_action in your sending notification server(my example is PHP)

$notification= array(
                'title'     => '快訊',
                "body" => "市場大特價",
                'tickerText'    => 'Ticker text here',
                'vibrate'   => 1,
                'sound'     => 1,
                'largeIcon' => 'large_icon',
                'smallIcon' => 'small_icon',
                'click_action' => 'AnyNameYouWant');
查看更多
冷血范
4楼-- · 2019-02-27 04:25

Use this is onCreate method of your MainActivity

        if (getIntent().getExtras() != null) { 
        // I Wrote here. 
        Intent intent = new Intent(MainActivity.this, NotificationActivity.class); 
        startActivity(intent); 
    } 
查看更多
Bombasti
5楼-- · 2019-02-27 04:28

If you force stop an app you don't get any notifications until the app is restarted. None of your code will work until the app is restarted.

All services of the app will also be stopped until user manually launches the app again

查看更多
We Are One
6楼-- · 2019-02-27 04:30

To send a message using API, you can use a tool called AdvancedREST Client.

{ "data": {
"image": "https://ibin.co/2t1lLdpfS06F.png",
"message": "Firebase Push Message Using API"
"AnotherActivity": "True" },"to" : "f25gYF3***********************HLI"}
查看更多
登录 后发表回答