parse push notification language with cloud code

帅比萌擦擦* 提交于 2019-12-25 16:44:30

问题


My app needs to be bilingual (english and italian). I'm working on push notifications through cloud code and i'm trying to send different notifications based on the client language. I created a language field in the Installation table and saved to it the [[NSLocale preferredLanguages] objectAtIndex:0];. The code below works but i wonder if there is another way to do it. I would prefer to set the "alert" message before the query so that i would have only 1 query. Basically i need to check if the language field for that particular user is "it" or not and then make the query. Is it possible or mine is the only solution?

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

回答1:


Yes, there is. You have to set the aps dictionary of the push notification payload directly and use the loc-key and optionally the loc-args and action-loc-key parameters. In the first parameter you pass the localization key of the message that you have localized in your Localizable.strings file in your application bundle. In the second argument you can pass an array that will be substituted to the string placeholders in the localized message. The third argument will be used as the name of the default action ("slide to …")

For example you define in your Localizable.stings file the following key:

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

And in cloud code you construct your push payload as follows:

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

This code should show you "John wants to send you a message", localized to the current locale of the user and the default action would be "slide to respond…"



来源:https://stackoverflow.com/questions/26163121/parse-push-notification-language-with-cloud-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!