【问题标题】:parse push notification language with cloud code使用云代码解析推送通知语言
【发布时间】:2014-10-02 14:30:31
【问题描述】:

我的应用需要是双语的(英语和意大利语)。我正在通过云代码处理推送通知,并尝试根据客户端语言发送不同的通知。我在安装表中创建了一个语言字段并将其保存到 [[NSLocale preferredLanguages] objectAtIndex:0];。下面的代码有效,但我想知道是否有另一种方法可以做到这一点。我宁愿在查询之前设置“警报”消息,这样我就只有 1 个查询。基本上我需要检查该特定用户的语言字段是否为“它”,然后进行查询。有可能还是我是唯一的解决方案?

//push test
Parse.Cloud.afterSave("MeetingObject", function(request) {
// user owner of the meeting object
var user = request.object.get("user");

var pushQueryEn = new Parse.Query(Parse.Installation);  
pushQueryEn.equalTo("user", user);
pushQueryEn.notEqualTo("language", 'it');
Parse.Push.send({
    where: pushQueryEn,
    data: {
        alert: "English push test",
        badge: "Increment",
        sound: "cheering.caf",
    }
}, {
    success: function() {
        // Push was successful
        console.log(request.object.get("language"));
    },
    error: function(error) {
        console.error("Got an error " + error.code + " : " + error.message);
    }
});

var pushQueryIt = new Parse.Query(Parse.Installation);  
pushQueryIt.equalTo("user", user);
pushQueryIt.equalTo("language", 'it');
Parse.Push.send({
    where: pushQueryIt,
    data: {
        alert: "Italian push test",
        badge: "Increment",
        sound: "cheering.caf",
    }
}, {
    success: function() {
        // Push was successful
        console.log(request.object.get("language"));
    },
    error: function(error) {
        console.error("Got an error " + error.code + " : " + error.message);
    }
});
});

【问题讨论】:

  • 可以添加iOS代码吗?

标签: ios push-notification parse-platform parse-cloud-code


【解决方案1】:

是的,有。您必须直接设置推送通知负载的aps 字典并使用loc-key 以及可选的loc-argsaction-loc-key 参数。在第一个参数中,您传递您在应用程序包中的Localizable.strings 文件中本地化的消息的本地化密钥。在第二个参数中,您可以传递一个数组,该数组将替换为本地化消息中的字符串占位符。第三个参数将用作默认操作的名称(“滑动到...”)

例如,您在 Localizable.stings 文件中定义以下键:

"msg" = "%@ wants to send you a message";
"rsp" = "respond";

在云代码中,您按如下方式构建推送负载:

var payload = 
  "data":{  
    "aps":{  
       "alert":{  
          "loc-key":"msg",
          "loc-args":["John"],
          "action-loc-key":"rsp"
       },
    }
  };
// set at least the 'where' key of the payload
Parse.Push.send(payload);

此代码应显示“John 想向您发送消息”,本地化为用户的当前语言环境,默认操作为“滑动响应...”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多