description |
---|
This section describes how to send a notification using the Push SDK. |
{% hint style="info" %} Check out push-for-hackers/sdk-functionality repo for code coverage of all SDK functions. Check out @pushprotocol/restapi or @pushprotocol/socket for more examples. {% endhint %}
Install the sdk restapi
package and its peer dependencies in your app.
{% tabs %} {% tab title="npm" %}
npm install @pushprotocol/restapi@latest ethers
{% endtab %}
{% tab title="yarn" %}
yarn add @pushprotocol/restapi@latest ethers
{% endtab %} {% endtabs %}
{% hint style="warning" %}
Note: If you wish to use ES6 Modules syntax, then inside package.json
set “type” to “module”.
{% endhint %}
import * as PushAPI from "@pushprotocol/restapi";
In the code below, we'll send a notification to all subscribers of your channel, a broadcast notification. For different types of notifications and more examples, go to this page.
{% hint style="info" %}
The PKey
is the private key from the wallet you created a channel. The channel address
is going to be the address of the wallet you created your channel.
{% endhint %}
import * as PushAPI from "@pushprotocol/restapi";
import * as ethers from "ethers";
const PK = 'your_channel_address_secret_key'; // channel private key
const Pkey = `0x${PK}`;
const _signer = new ethers.Wallet(Pkey);
const sendNotification = async() => {
try {
const apiResponse = await PushAPI.payloads.sendNotification({
signer: _signer,
type: 1, // broadcast
identityType: 2, // direct payload
notification: {
title: `[SDK-TEST] notification TITLE:`,
body: `[sdk-test] notification BODY`
},
payload: {
title: `[sdk-test] payload title`,
body: `sample msg body`,
cta: '',
img: ''
},
channel: 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', // your channel address
env: 'staging'
});
} catch (err) {
console.error('Error: ', err);
}
}
sendNotification();
For sending different types of notifications, such as Targeted and Subset, and for more examples, go to the page below:
{% content-ref url="../../developer-tooling/push-sdk/sdk-packages-details/epnsproject-sdk-restapi/for-notification/send-notifications.md" %} send-notifications.md {% endcontent-ref %}