parse push notification language with cloud code

2019-09-12 02:24发布

问题:

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…"