Push Notifications
अपने उपयोगकर्ताओं को Web Push notifications भेजें। SDK से सेट अप करें और AI को सब कुछ wire up करने दें।
हर Proyecta app अपने उपयोगकर्ताओं को browser और PWA push notifications भेज सकती है। Proyecta SDK VAPID key generation, subscription storage, topic management और delivery संभालता है — किसी third-party account की ज़रूरत नहीं।
यह कैसे काम करता है
Section titled “यह कैसे काम करता है”Proyecta में push notifications, standard Web Push lifecycle के चार चरणों का पालन करती हैं:
- Enable — अपने app के लिए VAPID keys generate करें (एक बार)
- Subscribe — notifications receive करने के लिए किसी user के device को register करें
- Identify — किसी anonymous subscription को signed-in user से जोड़ें
- Send — specific users, topics या सभी को notification deliver करें
ये चारों SDK में proyecta.pushNotifications के अंतर्गत उपलब्ध हैं।
अपने app के लिए push enable करें
Section titled “अपने app के लिए push enable करें”पहली call एक VAPID public key generate करती है जिसकी ज़रूरत browser को subscription बनाने के लिए होती है। यह idempotent है — इसे दोबारा call करने पर वही key वापस मिलती है।
import Proyecta from '@proyecta-ai/sdk';
const proyecta = new Proyecta({ apiKey: process.env.PROYECTA_API_KEY });
const { vapidPublicKey } = await proyecta.pushNotifications.enable();AI से पूछें: "Enable push notifications and store the VAPID key in a PROYECTA_VAPID_PUBLIC_KEY env var".
किसी user के device को subscribe करें
Section titled “किसी user के device को subscribe करें”Client पर VAPID key के साथ एक Web Push subscription बनाएं, फिर उसे visitorId के साथ server पर भेजें:
// client sideconst registration = await navigator.serviceWorker.ready;const subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: PROYECTA_VAPID_PUBLIC_KEY,});
// server side (or via your API route)const { secret } = await proyecta.pushNotifications.subscribe({ visitorId: currentUser?.id ?? generateAnonymousId(), subscription: { endpoint: subscription.endpoint, keys: { auth: subscription.keys.auth, p256dh: subscription.keys.p256dh, }, },});// `secret` को client पर store करें — बाद में unsubscribe या identify करने के लिए यह ज़रूरी हैअगर user signed in है तो visitorId के रूप में authenticated user ID का उपयोग करें, या anonymous visitors के लिए एक generated UUID का।
Anonymous subscriptions को users से जोड़ें
Section titled “Anonymous subscriptions को users से जोड़ें”जब कोई anonymous visitor sign in करे, तो उनकी मौजूदा subscription को authenticated user ID से link करें ताकि अगली send call उन्हें target कर सके:
await proyecta.pushNotifications.identify({ secret: storedSubscriptionSecret, userId: authenticatedUser.id,});Notification भेजें
Section titled “Notification भेजें”आप specific visitor IDs, topics, दोनों (union के रूप में) को target कर सकते हैं, या सभी को broadcast कर सकते हैं। सभी को broadcast करने के लिए visitorIds और topics दोनों को हटा दें।
// Send to specific usersawait proyecta.pushNotifications.send({ title: 'Your order has shipped', body: 'Track it from your dashboard', visitorIds: ['user_123', 'user_456'], data: { orderId: 'ord_789' }, // custom payload});
// Send to everyone subscribed to a topicawait proyecta.pushNotifications.send({ title: 'New feature just dropped', topics: ['product-updates'], icon: 'https://cdn.example.com/icon.png', image: 'https://cdn.example.com/banner.png',});
// Broadcast to all subscribersawait proyecta.pushNotifications.send({ title: 'Scheduled maintenance at 2am UTC',});send { sent, failed } return करता है ताकि आप जान सकें कि delivery कैसी रही। मौजूदा stub response में sent count targeted stored subscriptions की संख्या दर्शाता है, actual browser deliveries की नहीं — production delivery सक्षम होने तक इसे delivery confirmation के रूप में न मानें।
Topics
Section titled “Topics”उपयोगकर्ता proyecta.pushNotifications.topics sub-resource के ज़रिए named topics (जैसे product-updates, weekly-digest) को subscribe कर सकते हैं। broadcast-style updates के लिए opt-in lists manage करने का यह recommended तरीका है — आपको खुद visitor IDs track नहीं करने पड़ते।
Unsubscribe करें
Section titled “Unsubscribe करें”subscribe से मिले secret का उपयोग करके subscription हटाएं:
await proyecta.pushNotifications.unsubscribe({ secret });AI से wire up करवाएं
Section titled “AI से wire up करवाएं”यह सब खुद लिखने की ज़रूरत नहीं है। AI से पूछें:
"Enable push notifications for my app. Register a service worker, call pushNotifications.subscribe on the client after the user grants permission, and store the secret in localStorage.""Send a push to all users subscribed to the 'breaking-news' topic whenever a new article is published.""After a user signs in, call pushNotifications.identify to link their anonymous subscription to their user ID."
ध्यान देने योग्य बातें
Section titled “ध्यान देने योग्य बातें”- Preview में real pushes नहीं जातीं। Push notifications केवल आपके app के published version पर काम करती हैं क्योंकि browsers को एक real origin पर HTTPS की ज़रूरत होती है।
- iOS के लिए PWA ज़रूरी है। iOS 16.4+ पर, Web Push केवल एक installed Progressive Web App (home screen पर add की गई) से काम करता है, सीधे Safari से नहीं।
- Permission एक बार का मौका है। अगर कोई user notification permission deny कर देता है, तो browsers आपको programmatically दोबारा prompt करने की अनुमति नहीं देते। आपको उन्हें अपने browser या device settings में जाने के लिए guide करना होगा।
जल्द आने वाला है
Section titled “जल्द आने वाला है”- Scheduled और event-triggered sends — app events या schedule के आधार पर automatically भेजें
- Deep linking — notification tap करने पर app में एक specific screen खोलें