Firebase onMessageReceived not called when app is inactive

前端 未结 5 1106
醉话见心
醉话见心 2021-01-22 05:23

I am using Firebase Cloud Messaging on Android.

  • I can successfully receive notification and data messages when the app is in the foreground.
  • The documen
相关标签:
5条回答
  • 2021-01-22 05:53

    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

    0 讨论(0)
  • 2021-01-22 05:55

    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); 
        } 
    
    0 讨论(0)
  • 2021-01-22 06:01

    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.

    0 讨论(0)
  • 2021-01-22 06:05

    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"}
    
    0 讨论(0)
  • 2021-01-22 06:09

    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');
    
    0 讨论(0)
提交回复
热议问题