จัดการความยินยอมของผู้ใช้สำหรับการแจ้งเตือนแบบพุชบนเว็บ
โดยค่าเริ่มต้น Pushwoosh SDK จะแสดงกล่องข้อความขอสมัครรับข้อมูล (native subscription prompt) ทันทีที่เริ่มต้นการทำงาน อย่างไรก็ตาม คุณอาจต้องการขอความยินยอมจากผู้ใช้ในเวลาที่เหมาะสมกว่า เช่น หลังจากที่ผู้ใช้มีโอกาสโต้ตอบกับเว็บไซต์ของคุณแล้ว
การปิดใช้งานการสมัครรับข้อมูลอัตโนมัติ
Anchor link toเพื่อป้องกันไม่ให้ SDK แสดงกล่องข้อความขอสมัครรับข้อมูลโดยอัตโนมัติเมื่อเริ่มต้นการทำงาน ให้ตั้งค่าพารามิเตอร์ communicationEnabled
เป็น false
ในการเรียกใช้ init
ซึ่งจะทำให้คุณสามารถควบคุมได้ว่าจะขอสิทธิ์การแจ้งเตือนแบบพุชเมื่อใด
<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เมื่อคุณปิดใช้งานการสมัครรับข้อมูลอัตโนมัติแล้ว คุณสามารถแจ้งให้ผู้ใช้สมัครรับข้อมูลได้ตลอดเวลา เมื่อผู้ใช้ตกลงที่จะรับการแจ้งเตือนแบบพุช (เช่น โดยการคลิกปุ่ม “Subscribe” บน UI ที่คุณกำหนดเอง) คุณสามารถเปิดใช้งานการสื่อสารได้โดยการเรียกใช้เมธอด setCommunicationEnabled
Pushwoosh.setCommunicationEnabled(status?: boolean): Promise<void>
การเรียกใช้ Pushwoosh.setCommunicationEnabled(true)
จะเป็นการเปิดใช้งานการสื่อสารกับบริการของ Pushwoosh เมื่อเปิดใช้งานแล้ว SDK จะดำเนินการแสดงกล่องข้อความขออนุญาตของเบราว์เซอร์ (native browser permission prompt) ต่อไป
นี่คือตัวอย่างวิธีการใช้งานเมธอดนี้:
// 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); });});