自定义百度推送JSON有效负载

时间:2017-03-04 03:42:46

标签: android json rest push-notification baidu

我正在使用rest API发送百度推送通知。我无法访问Android的通知构建器类,因此我不能使用# Define advanced function that mistakenly names a a parameter for # a common parameter: PS> function foo { [CmdletBinding()] param([string] $Verbose) 'hi' } # NO error is reported at this point. # Later invocation of the function, including introspection of its parameters # when you pass it to Get-Help, then surfaces the problem: PS> Get-Help foo Get-Help : A parameter with the name 'Verbose' was defined multiple times for the command. At line:1 char:1 + Get-Help foo + ~~~~~~~~~~~~ + CategoryInfo : MetadataError: (:) [Get-Help], MetadataException + FullyQualifiedErrorId : ParameterNameAlreadyExistsForCommand,Microsoft.PowerShell.Commands.GetHelpCommand 。我需要知道的主要事情是如何使用JSON将百度推送通知的优先级设置为MAX。

根据this website,以下是百度JSON有效负载的所有键和值:

notificationBuilder.setPriority(2);

我假设我需要设置{ //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" } 的值,但因为它只需要一个整数作为值,我不知道如何创建与id对应的通知。

1 个答案:

答案 0 :(得分:0)

我能够找到如何将优先级设置为高,以使通知显示为横幅,而不是状态栏中的图标。

在客户端应用程序中,您需要设置通知构建器,并为其指定ID(请注意我使用的是Xamarin Forms,但如果您使用的是原生Android,则逻辑相同。此外,此代码应直接放在PushManager.StartWork()方法调用之后,最有可能在您的活动的onCreate()方法中):

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);

然后对于有效载荷,它就像这样:

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

再次注意,notification_builder_id的值必须与SetNotificationBuilder()方法中的第二个参数相对应。