จัดการความยินยอมของผู้ใช้สำหรับการแจ้งเตือนแบบ Web Push
โดยค่าเริ่มต้น Pushwoosh SDK จะแสดงข้อความแจ้งการสมัครสมาชิกแบบเนทีฟทันทีที่เริ่มต้น อย่างไรก็ตาม คุณอาจต้องการขอความยินยอมของผู้ใช้ในเวลาที่เหมาะสมกว่า เช่น หลังจากที่ผู้ใช้มีโอกาสโต้ตอบกับเว็บไซต์ของคุณแล้ว
การปิดใช้งานการสมัครสมาชิกอัตโนมัติ
Anchor link toเพื่อป้องกันไม่ให้ SDK แสดงข้อความแจ้งการสมัครสมาชิกโดยอัตโนมัติเมื่อเริ่มต้น ให้ตั้งค่าพารามิเตอร์ communicationEnabled เป็น false ในการเรียก init ซึ่งจะช่วยให้คุณควบคุมได้ว่าจะขอสิทธิ์การแจ้งเตือนแบบ push เมื่อใด
<script type="text/javascript" src="//cdn.pushwoosh.com/webpush/v3/pushwoosh-web-notifications.js" async></script><script type="text/javascript"> var Pushwoosh = Pushwoosh || []; Pushwoosh.push(['init', { // other initialization parameters... communicationEnabled: false, // Disable communication to prevent automatic subscription prompts }]);</script>การเปิดใช้งานการสมัครสมาชิกเมื่อผู้ใช้ให้ความยินยอม
Anchor link toเมื่อคุณปิดใช้งานการสมัครสมาชิกอัตโนมัติแล้ว คุณสามารถแจ้งให้ผู้ใช้สมัครสมาชิกได้ตลอดเวลา เมื่อผู้ใช้ตกลงที่จะรับการแจ้งเตือนแบบ push (เช่น โดยการคลิกปุ่ม “Subscribe” บน UI ที่กำหนดเองของคุณ) คุณสามารถเปิดใช้งานการสื่อสารได้โดยการเรียกเมธอด setCommunicationEnabled
Pushwoosh.setCommunicationEnabled(status?: boolean): Promise<void>
การเรียก Pushwoosh.setCommunicationEnabled(true) จะเปิดใช้งานการสื่อสารกับบริการ Pushwoosh เมื่อเปิดใช้งานแล้ว SDK จะดำเนินการแสดงข้อความแจ้งสิทธิ์ของเบราว์เซอร์แบบเนทีฟ
นี่คือตัวอย่างวิธีการใช้เมธอดนี้:
// Assuming you have a subscribe button with id="subscribe-button"const subscribeButton = document.getElementById('subscribe-button');
subscribeButton.addEventListener('click', () => { Pushwoosh.setCommunicationEnabled(true) .then(() => { console.log('User is subscribed to push notifications.'); // You can hide the subscribe button now subscribeButton.style.display = 'none'; }) .catch((error) => { console.error('Error subscribing user:', error); });});