自定义百度推送 JSON 负载
Customize Baidu Push JSON Payload
我正在休息API发送百度推送通知。我无权访问 Android 的通知生成器 类,因此我不能,例如,使用 notificationBuilder.setPriority(2);
。我真正需要知道的主要事情是如何将百度推送通知的优先级设置为 MAX,仅使用 JSON。
根据 this website,以下是百度 JSON 有效负载的所有键和值:
{
//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"
}
我假设我需要设置 notification_builder_id
的值,但由于它只是将整数作为值,我不确定如何创建与 id 相对应的通知。
我找到了如何将优先级设置为高的方法,以便使通知显示为横幅,而不仅仅是状态栏中的图标。
在client app中,你需要设置notification builder,并给它一个id,就像这样(注意我用的是Xamarin Forms
,但是如果你用的是native,逻辑是一样的Android。此外,此代码应直接放在 PushManager.StartWork()
方法调用之后,很可能在您的 activity 的 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()
方法中的第二个参数相对应。
我正在休息API发送百度推送通知。我无权访问 Android 的通知生成器 类,因此我不能,例如,使用 notificationBuilder.setPriority(2);
。我真正需要知道的主要事情是如何将百度推送通知的优先级设置为 MAX,仅使用 JSON。
根据 this website,以下是百度 JSON 有效负载的所有键和值:
{
//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"
}
我假设我需要设置 notification_builder_id
的值,但由于它只是将整数作为值,我不确定如何创建与 id 相对应的通知。
我找到了如何将优先级设置为高的方法,以便使通知显示为横幅,而不仅仅是状态栏中的图标。
在client app中,你需要设置notification builder,并给它一个id,就像这样(注意我用的是Xamarin Forms
,但是如果你用的是native,逻辑是一样的Android。此外,此代码应直接放在 PushManager.StartWork()
方法调用之后,很可能在您的 activity 的 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()
方法中的第二个参数相对应。