Parse Push Notification Include Users TimeZone in

2019-07-15 03:04发布

问题:

Is it possible to include the users' timezone if I want to send a push notification containing a datetime?

Parse.Push.send({
    where: query, // Set our Installation query
    data: {
        type: 2, // new applications push
        applicationID: applicationID,
        startTime: startTime,
        "alert" : {
            "title-loc-key": "SUCCESSFUL_TITLE",
            "loc-key": "SUCCESSFUL_MESSAGE",
            "loc-args": [startTime]
        },
        "sound": "default",
        "content-available" : "1",
        "badge" : 'Increment'
    }

So the push message displays a time like "[some text] at 01.02.2016, 19:00 Uhr". I hardcoded a timezone with moment.js in the cloudcode - but for the future, different users from different regions should get date and time information in their respective timezones... Is it possible to achieve this with the Parse.Push call and without manual iteration over the Installation table?

回答1:

According to the Parse.com documentation, there is an Installation object in which you can get the user's time zone via the timeZone parameter.

timeZone: The current time zone where the target device is located. This value is synchronized every time an Installation object is saved from the device.

Elsewhere in the documentation for their REST API, they talk about local push scheduling, and describe the Installation.timeZone parameter in terms of IANA time zone IDs, such as America/New_York or America/Los_Angeles. These IDs are the same ones that moment-timezone uses.

So first you need to load moment and moment-timezone, along with some time zone data. Probably you want the moment-timezone-2010-2020.js file.

Then you can do something like this:

var timeZone = Installation.timeZone; // or however you get this from Parse.com

var startTime = '2015-09-27T12:00:00Z';  // ISO-8601 format

var m = moment(startTime).tz(timeZone);  // converted to the user's time zone
var s = m.calendar();  // or m.format, or whatever you want

var message = "Hi Bob, your event starts " + s;

Then push out the message as you showed in your question