Parse Push Notification Include Users TimeZone in Push-Time

心不动则不痛 提交于 2019-12-11 03:44:24

问题


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



来源:https://stackoverflow.com/questions/32783818/parse-push-notification-include-users-timezone-in-push-time

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