Quickstart
Everything you will need to get up and running in 2 mins or less!
Installation
- js
- react
- reactnative
# Install Libraries
npm install @pushprotocol/restapi@latest ethers@^5.7
# Install Libraries
npm install @pushprotocol/restapi@latest ethers@^5.7
# Install Libraries
npm install @pushprotocol/restapi@latest ethers@^5.7
Import libraries
- js
- react
- reactnative
// Import restapi for function calls
import { PushAPI, CONSTANTS } from "@pushprotocol/restapi";
// Ethers v5 or Viem, both are supported
import { ethers } from "ethers";
// Import restapi for function calls
import { PushAPI, CONSTANTS } from "@pushprotocol/restapi";
// Ethers v5 or Viem, both are supported
import { ethers } from "ethers";
// Import restapi for function calls
import { PushAPI, CONSTANTS } from "@pushprotocol/restapi";
// Ethers v5 or Viem, both are supported
import { ethers } from "ethers";
Initialize User
- js
- react
- reactnative
// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();
// Initialize wallet user
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, { env: CONSTANTS.ENV.STAGING });
// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();
// Initialize wallet user
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, { env: CONSTANTS.ENV.STAGING });
// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();
// Initialize wallet user
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, { env: CONSTANTS.ENV.STAGING });
Send Notifcation
- js
- react
- reactnative
// Requires user to have a channel, see Create Channel section for more info
// ['*'] sends to all wallets who have opted in to your channel
const response = await userAlice.channel.send(["*"], {
notification: {
title: "You awesome notification",
body: "from your amazing protocol",
},
});
// Requires user to have a channel, see Create Channel section for more info
// ['*'] sends to all wallets who have opted in to your channel
const response = await userAlice.channel.send(["*"], {
notification: {
title: "You awesome notification",
body: "from your amazing protocol",
},
});
// Requires user to have a channel, see Create Channel section for more info
// ['*'] sends to all wallets who have opted in to your channel
const response = await userAlice.channel.send(["*"], {
notification: {
title: "You awesome notification",
body: "from your amazing protocol",
},
});
Stream / Real time updates / Socket
- js
- react
- reactnative
// To listen to real time notifications
const stream = await userAlice.initStream([CONSTANTS.STREAM.NOTIF]);
// Setup event handling
stream.on(CONSTANTS.STREAM.NOTIF, (data) => {
console.log(data);
});
// Connect stream but only after setting all event handling
stream.connect();
// To listen to real time notifications
const stream = await userAlice.initStream([CONSTANTS.STREAM.NOTIF]);
// Setup event handling
stream.on(CONSTANTS.STREAM.NOTIF, (data) => {
console.log(data);
});
// Connect stream but only after setting all event handling
stream.connect();
// To listen to real time notifications
const stream = await userAlice.initStream([CONSTANTS.STREAM.NOTIF]);
// Setup event handling
stream.on(CONSTANTS.STREAM.NOTIF, (data) => {
console.log(data);
});
// Connect stream but only after setting all event handling
stream.connect();