Search code examples
androidjsonrestpush-notificationbaidu

Customize Baidu Push JSON Payload


I am using a rest API to send Baidu Push notifications. I don't have access to Android's notification builder classes, so I can't, for example, use notificationBuilder.setPriority(2);. Really the main thing that I need to know is how to set the priority of a Baidu Push notification to MAX, using JSON only.

According to this website, here are all of the keys and values for a Baidu JSON payload:

{
    //android必选,ios可选
    "title": "hello" ,   
    "description": "hello world" 

    //android特有字段,可选
    "notification_builder_id": 0,
    "notification_basic_style": 7,
    "open_type":0,
    "net_support" : 1,
    "user_confirm": 0,
    "url": "http://developer.baidu.com",
    "pkg_content":"",
    "pkg_name" : "com.baidu.bccsclient",
    "pkg_version":"0.1",

    //android自定义字段
    "custom_content": {
        "key1":"value1", 
        "key2":"value2"
    },  

    //ios特有字段,可选
    "aps": {
        "alert":"Message From Baidu Push",
       "sound":"",
        "badge":0
    },

    //ios的自定义字段
    "key1":"value1", 
    "key2":"value2"
}

I assume I need to set the values of notification_builder_id, but as it simply takes an integer as a the value, I am not sure how to create the notification to correspond with the id.


Solution

  • I was able to find out how to set the priority to high, in order to make the notification appear as a banner, as opposed to just an icon in the status bar.

    In the client app, you need to set the notification builder, and give it an id, like so (note that I'm using Xamarin Forms, but the logic is the same if you are using native Android. Also, this code should be placed directly after the PushManager.StartWork() method call, most likely in your activity's onCreate() method):

    BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder();
    builder.SetNotificationFlags(
        (int)NotificationFlags.AutoCancel |
        (int)NotificationFlags.HighPriority |
        (int)NotificationFlags.ShowLights);
    builder.SetNotificationDefaults(
        (int)NotificationDefaults.Lights |
        (int)NotificationDefaults.Vibrate);
    builder.SetStatusbarIcon(Resource.Drawable.icon);
    
    // the second parameter is the id. This is the id that you need to set
    // in the JSON payload in order for the incoming notification to appear
    // in this way.
    PushManager.SetNotificationBuilder(Xamarin.Forms.Forms.Context, 1, builder);
    

    And then for the payload, its simply like this:

    {"title":"My Title","description":"my description","notification_builder_id":1}
    

    Again, note that the value of notification_builder_id has to correspond with the second parameter in the SetNotificationBuilder() method.