Notification API How To Use

var note = new Notification({
    title: "Open Website",
    url: "https://scriptable.app"
})

This is my first time using the Notification API and I don’t know what went wrong. I expected it to send me a notification to Scriptable.app but it did nothing. What does the Notification API do exactly? There were no errors and no logs in the console.

Run this example I put together. It will display an informational notification and then 10 seconds later open a notification that will open the Automators forum when you tap on it.

const delaySeconds = 10;

let currentDT = new Date;
let newDT = new Date(currentDT.getTime() + (delaySeconds * 1000));
	
let notify1 = new Notification();
notify1.title = "Upcoming Notification";
notify1.body = "An actionable notification coming up in " + delaySeconds + " seconds.";
await notify1.schedule();

let notify2 = new Notification();
notify2.title = "Open Website";
notify2.body = "Open the Automators forum";
notify2.openURL = "https://talk.automators.fm/";
notify2.setTriggerDate(newDT);
await notify2.schedule();

Hope that helps.

Can I add an action button to a notification which will display an alert when clicked?

You can run a script that could show an alert. No in-notification buttons and no in-notification running of code and UI elements.

Thanks for the answer.
I created a notification that reminds me of taking some water but it is not displayed in my Apple Watch. Any way to do that?

In the watch app on your phone, look under notifications and ensure that you have notifications for Scriptable enabled there.

let n = Device.batteryLevel()
console.log(n100)
if (n < 30) {
let notify = new Notification()
notify.title = “Battery Low”
notify.subtitle = “Please charge your device” + ",battery is " + Math.round(n
100) + “%”
notify.schedule()
}

Can u pls tell me what is wrong in this code?
It is supposed to show a notification if battery level is less than 30% but the if block is not executing as it should.

Thanks in advance

  1. What is n100? I don’t see it defined anywhere?
  2. What is in the console log when you run it?

Also

  • You can put code blocks between triple back ticks to format them.
  • Consider writing your new questions as new topics rather than on the end of someone else’s topic.

n 100 is actually n multiply by 100.
console.log is just for my reference

Have you defined that elsewhere? It isn’t defined in your code above.

The console log will contain any errors that occur. You just happen to be using it to log some output. Hence why I asked what is in it. If your code is failing to run, the log often explains why it is failing.

Is there a way to run the script every 30mins in the background and notify me what happens?

You could have the script create a new notification for 30 minutes time when it completes, but you would probably want some extra logic around that as repeating every 30 mins 24x7 is probably not what you would really want.

I have an app that uses a cron approach to do this. */30 * * * *, it is easy to run my script every 30 mins. But I do not know which API should I use in this app.

Well there is no cron app available on iOS, so I assume you would want the Scriptable notification object.

https://docs.scriptable.app/notification/

@sylumer
Thank you. I have found a way to do this. It use the Timer API. The timer repeats every 30mins and fires the callback function. This is the code, you could reduce the timeinterval to several seconds to verify.

let t = new Timer();
t.timeInterval = 30*60*1000;//mins*secs*ms
t.repeats = true;
t.schedule(()=>{
    console.log(getTime());
});

function getTime() {
    let time = new Date();
    return time.toLocaleDateString()+' '+time.toLocaleTimeString('en-us',{hour12:false})
}

em…The code seems not keep running after I add it to the shortcut…

1 Like