Notifications
You can display notifications directly in MetaMask or natively in a user's operating system (OS)
using the snap_notify
API method.
Steps
1. Request permission to notify users
Request the snap_notify
permission.
Add the following to your Snap's manifest file:
"initialPermissions": {
"snap_notify": {}
}
2. Create a notification
Create a notification by calling snap_notify
, which takes
a notification type
and message
.
Specify type: "inApp"
to display the notification in the MetaMask UI, or type: "native"
to
display the notification in the user's OS.
We recommend using type: "inApp"
because there's no guarantee that native notifications are
displayed to the user.
You can also call snap_notify
twice, which each notification type, to trigger both an in-app and
native notification.
The following example displays a notification in MetaMask, with the message "Hello, world!":
await snap.request({
method: "snap_notify",
params: {
type: "inApp",
message: "Hello, world!",
},
});
Each Snap can trigger up to:
- Five in-app notifications per minute.
- Two native notifications per five minutes.
Example
See the
@metamask/notifications-example-snap
package for a full example of implementing notifications using
snap_notify
.
This example exposes a custom JSON-RPC API for
dapps to display in-app and native notifications.