2014-10-04 76 views
0

解析的文檔從雲代碼發送推送通知的規定,格式如下:如何在不使用日期的情況下在特定時間發送解析推送通知?

Parse.Push.send({ 
    where: query, 
    push_time: new Date("2013-08-26T12:00:00"), 
    data: { 
     alert: "Local push notification" 
    } 
}, { 
    success: function() { 
     console.log("Successful push"); 
    }, 
    error: function(error) { 
     console.log(error); 
    } 
}); 

我可能會誤解這一點,但看起來這將告訴解析不僅在發送推送通知當地時間爲所有用戶的12:00:00,還包括以下日期:2013-08-26

當函數運行時,我想要做的就是在當地時間發送通知,無論函數運行的日期是什麼。我不知道如何正確設置語法來執行此操作,因爲push_time: new Date("T12:00:00");似乎無法正常工作。

+0

您是否找到解決方案? – 2015-01-19 10:26:56

回答

0

我有兩個答案,你可以嘗試。他們都要求你指定日期和時間,但你可以使用明天(或真正的任何一天)快速設置的日期方面。

首先,使用原生的JavaScript SDK:

var tomorrow = new Date(); 
tomorrow.setDate(tomorrow.getDate() + 1); 
tomorrow.setHours(getRandomArbitrary(0,23),getRandomArbitrary(0,59),getRandomArbitrary(0,59)); 
var dayAfterTomorrow = new Date(); 
dayAfterTomorrow.setDate(tomorrow.getDate() + 1); 

Parse.Push.send({ 
    channels: [ "my channel" ], 
    data: { 
     alert: "push content here" 
    },  
    push_time: tomorrow, 
    expiration_time: dayAfterTomorrow 
    }, {  
    success: function() { 
     // Push was successful 
     console.log("successfully sent push out"); 
     status.success("Push away!"); 
    }, 
    error: function(error) { 
     // Handle error 
     console.log("got an error"); 
     console.log("Error: " + error.code + " " + error.message); 
    } 
}); 

function getRandomArbitrary(min, max) { 
    return Math.random() * (max - min) + min;  
} 

其次,根據this Parse post from a year ago,還有他們的SDK中的JavaScript,不會做本地時區推的錯誤。你需要使用他們的REST API:

Parse.Cloud.httpRequest({ 
    method: "POST", 
    headers: { 
    "X-Parse-Application-Id": YOUR_APP_ID, 
    "X-Parse-REST-API-Key": REST_API_KEY, 
    "Content-Type": "application/json" 
    }, 
    body: { 
    "where": query, 
    "push_time": "2013-08-26T12:00:00", // format this as a STRING, not a Date Object 
    "data": {  
     "alert": "Local push notification" 
    } 
    }, 
    url: "https://api.parse.com/1/push" 
}).then(function() { 
    console.log("Successful push"); 
}, function(error) { 
    console.log(error); 
}); 

我沒有嘗試過的REST API代碼,但我知道JavaScript的SDK代碼工作,因爲我使用的是在我的一個項目。我不確定它是否將本地時間發送給用戶,但這不是我寫的項目的要求。

相關問題