[英]How to implement OneSignal Push Notifications in Nodejs?
我想使用 OneSignal 包在節點 js 中實現推送通知。
您可以按照以下步驟在 Nodejs 中實現 OneSignal 推送通知:
1- 首先在前端實現 OneSignal 推送通知,例如(React Native、Reactjs、Flutter)。
2- 然后通過從 One Signal 網站發送手動通知來測試這些推送通知。
手動測試鏈接: https ://dashboard.onesignal.com/apps/fade543c-d12e-422c-8e15-66afa731e2fe/notifications/new
3- 如果手動通知有效,那么我們可以繼續使用 Nodejs。 首先在您的節點應用程序中安裝“onesignal-node”包。
4- 並使用以下代碼發送通知
const sendPushNotificationsToAll = async (request, response) => {
\\you can find app id and api key in your one signal
application settings
const client = new OneSignal.Client(
process.env.ONE_SIGNAL_APP_ID,
process.env.ONE_SIGNAL_API_KEY
);
const notification = {
headings: { en: title }, ***title of notification***
contents: {
en: description, ***description of notification***
},
included_segments: ["Subscribed Users"], ***target audience***
large_icon: "http://www.logo.com", ***notification large logo***
big_picture: "http://www.bigpicture.com",
data: {
postId: postId, ***fields data***
},
};
try {
const res = await client.createNotification(notification);
response.status(200).json("Notification created successfully");
} catch (error) {
response.status(500).json(error);
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.