Firebase push notifications update DB

前端 未结 3 1632
萌比男神i
萌比男神i 2020-11-28 11:53

I\'m trying to send some data to my users via the new Firebase push notifications service.

I\'m adding some \"key-value\" custom data items with \'Message text\' -

3条回答
  •  醉梦人生
    2020-11-28 12:13

    In Android , if you want to update someting in the app whether the app is in forground/background/terminated states, then the best way is to remove the notification payload and send only the data payload

    { 
     "to" : "ee8....DYarin",
        "data" : {
             "key" : "value"
        }   
    }
    

    In this case onMessageReceived() will get executed. (In some android devices like xiomi you need to enable autostart for you application to process push in terminated states)

    In this case, how will you show notification to user ? You can pass title and body as custom tags in the data payload and generate notification programically. So in Android everything goes well without notification payload.


    And if you have notification payload in Android then the problems are:

    • If app is in forground everyting goes well, the notification is shown and onMessageReceived() is executed
    • If the app is in background/terminated states, then notification will be displayed on your device but onMessageReceived() will only work if you click on the notification. Users have the tendency to clear them often and our code in onMessageReceived() won't get executed.

    Also to enable this click functionality on notification, the notification payload should contain key named click_action:

    "notification":{
            "title":"Prime",
            "body" : "test",
             "click_action": "my_click_action_name"
    }
    

    And you need to register the action as intent_filter in you activity

     
                    
                    
     
    

    When ever user clicks on notification the onNewIntent() method of the curresponding activity is called and the data payload is available in the curresponding intent.getExtras()

提交回复
热议问题