Firebase onMessageReceived not called when app in background

前端 未结 26 3131
粉色の甜心
粉色の甜心 2020-11-22 02:32

I\'m working with Firebase and testing sending notifications to my app from my server while the app is in the background. The notification is sent successfully, it even appe

26条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 03:08

    As per Firebase Cloud Messaging documentation-If Activity is in foreground then onMessageReceived will get called. If Activity is in background or closed then notification message is shown in the notification center for app launcher activity. You can call your customized activity on click of notification if your app is in background by calling rest service api for firebase messaging as:

    URL-https://fcm.googleapis.com/fcm/send

    Method Type- POST

    Header- Content-Type:application/json
    Authorization:key=your api key
    

    Body/Payload:

    { "notification": {
        "title": "Your Title",
        "text": "Your Text",
         "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
      },
        "data": {
        "keyname": "any value " //you can get this data as extras in your activity and this data is optional
        },
      "to" : "to_id(firebase refreshedToken)"
    } 
    

    And with this in your app you can add below code in your activity to be called:

    
                    
                    
                
    

提交回复
热议问题