Skip to content

Instantly share code, notes, and snippets.

@liquidz
Created June 6, 2012 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liquidz/2882910 to your computer and use it in GitHub Desktop.
Save liquidz/2882910 to your computer and use it in GitHub Desktop.
Notify me today's weather every day the first time I unlock my phone after 5 AM
// Initializing variables
var dayBaseTime = 5; // (day - 1) if before 5 AM
var weatherOption = {
location: 'Tokyo'
, locationtype: 'city'
, days: 0
};
// End of variables initializing
var trim = function(str){
return str.replace(/(^\s*|\s*$)/, '');
};
var isNull = function(x){
return (x === null || x === undefined);
};
var getToday = function(date){
var today = new Date()
, year = today.getFullYear()
, month = today.getMonth() + 1
, day = today.getDate()
, hour = today.getHours()
;
if(hour < dayBaseTime){ --day; }
return('' + year + month + day);
};
console.log("Started script: Notify me today's weather every day the first time I unlock my phone before " + dayBaseTime + "AM");
device.screen.on('unlock', function(){
var lastUnlocked = device.localStorage.getItem('lastUnlocked');
var today = getToday();
// check last unlocked date
if(isNull(lastUnlocked) || lastUnlocked !== today){
// show weather notification
feeds.weather.get(weatherOption,
function(weather, status, response){
var now = weather.now
, alerts = trim('' + now.alerts)
;
var notify = device.notifications.createNotification("Today's Weather");
notify.content = now.sky+ '(rain: ' + now.rain + ')';
if(!isNull(alerts) && alerts !== ''){
notify.content += ' !! ' + alerts + ' !!';
}
notify.on('click', function(){
device.browser.launch(now.forecastUrl);
});
notify.show();
}, function(response, status){
console.error('Weather error from script');
});
// set last unlocked date
device.localStorage.setItem('lastUnlocked', today);
}
});
console.log("Completed script: Notify me today's weather every day the first time I unlock my phone before " + dayBaseTime + "AM");
@jessjpj
Copy link

jessjpj commented Sep 30, 2014

Is this possible in IOS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment