我们正在关注本教程:适用于Android的How To: Windows Azure Notification Hubs (Android Apps)。
在构建通知有效负载时,一切正常,如指南中所述。那就是:
{
"data": {
"msg": "$(property1)"
}
}
但是,我们希望扩展模板以在有效负载中使用多个自定义属性。类似的东西:
{
"data": {
"msg": {
"message": "$(property1)",
"sender": "$(property2)"
}
}
}
后端通过以下方式提供属性值:
Dictionary<string, string> templateValues = new Dictionary<string, string>
{
{ "property1", "Hello world" },
{ "property2", "foo" }
};
NotificationOutcome notificationOutcome = await Hub.SendTemplateNotificationAsync(templateValues, "test");
从移动应用在通知中心注册模板时,我们收到以下错误:
“提供的通知有效负载无效”
提前致谢
答案 0 :(得分:6)
有效内容无效,因为GCM不支持数据成员中的嵌套对象。 您可以通过注册以下模板来发送包含两个属性的消息:
{
"data": {
"message": "$(property1)",
"sender": "$(property2)"
}
}
在您的Android接收器中,您可以使用
检索您的属性intent.getStringExtra("property1");
答案 1 :(得分:0)
在我的测试中,您可以添加参数:
<强>模板:强>
private class SelectionAdapter extends ArrayAdapter<String> {
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public SelectionAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color
if (mSelection.get(position) != null) {
v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red
}
return v;
}
}
数据:强>
{
"data": {
"message": "$(property1)",
"args": "$(property2)",
"myargs": "$(property3)",
}
}
<强>结果:强>
{
"property1":"Jonh",
"property2":"1,1",
"property3":"0",
}