UNNotificationRequest
UNNotificationRequest represents a notification to be sent: an identifier, the content, and when to deliver it. Pass it to addNotificationRequest(), and the system delivers it according to its trigger.
The identifier is the notification's name, chosen by the app. Cancellation looks it up by identifier. If you submit another request with the same identifier, the system replaces the old one with the new one; there are never two.
A request is a read-only value: its fields can't be changed and no fields can be added; writing throws TypeError. A request read back from getPendingNotificationRequests() isn't the same object as the one you submitted, although its content is the same. Compare two requests by identifier, not with ===. It's a plain JS value and doesn't inherit from NSObject.
You need to install @nacrejs/user-notifications first. See UNUserNotificationCenter for how, or see Core and capability packages.
Inheritance: UNNotificationRequest
Constructor
You can't create one with new. new UNNotificationRequest() throws TypeError:
Illegal constructor: UNNotificationRequest cannot be constructed with new; use UNNotificationRequest.requestWithIdentifier()Static methods
UNNotificationRequest.requestWithIdentifier()
Creates a request from an identifier, content, and a trigger.
Instance properties
| Property | Description |
|---|---|
identifier read-only | The identifier |
content read-only | A copy of the content as of when the request was created |
trigger read-only | The trigger. null means deliver immediately |
Examples
const content = new UNMutableNotificationContent();
content.title = 'Time to drink water';
const request = UNNotificationRequest.requestWithIdentifier('water', {
content,
trigger: UNTimeIntervalNotificationTrigger.triggerWithTimeInterval(3600),
});
await UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request);Effect (not verified in the simulator): One hour later, a notification titled "Time to drink water" is delivered. If the user hasn't allowed notifications, the await line throws an error whose name is NotAllowedError.
Corresponding iOS API
| ObjC | JS |
|---|---|
[UNNotificationRequest requestWithIdentifier:@"water" content:content trigger:trigger] | UNNotificationRequest.requestWithIdentifier('water', { content, trigger }) |
See also
UNMutableNotificationContent: writing the contentUNTimeIntervalNotificationTrigger,UNCalendarNotificationTrigger: the two kinds of triggeraddNotificationRequest(): handing it to the system