ข้ามไปยังเนื้อหา

Web Push SDK 3.0

การผสานการทำงาน

Anchor link to

ตัวอย่างการผสานบน GitHub

รับ Pushwoosh Web Push SDK และแตกไฟล์ คุณควรจะมีไฟล์ต่อไปนี้:

Anchor link to
  • pushwoosh-service-worker.js

วางไฟล์ทั้งหมดนี้ไว้ในไดเรกทอรีราก (root) ระดับบนสุดของเว็บไซต์ของคุณ

Anchor link to

เริ่มต้นการทำงาน SDK:

Anchor link to
  1. รวม SDK จาก CDN ของเราแบบ อะซิงโครนัส
<script type="text/javascript" src="//cdn.pushwoosh.com/webpush/v3/pushwoosh-web-notifications.js" async></script>
  1. เริ่มต้นการทำงาน Web Push SDK และตรวจสอบให้แน่ใจว่าได้จัดคิวการเริ่มต้นการทำงานจนกว่า SDK จะโหลดเสร็จสมบูรณ์
<script type="text/javascript">
var Pushwoosh = Pushwoosh || [];
Pushwoosh.push(['init', {
logLevel: 'info', // ค่าที่เป็นไปได้: error, info, debug
applicationCode: 'XXXXX-XXXXX', // application code ของคุณจาก Pushwoosh Control Panel
apiToken: 'XXXXXXX', // Device API Token
safariWebsitePushID: 'web.com.example.domain', // สตริง reverse-domain ที่ไม่ซ้ำกัน ซึ่งได้รับจาก Apple Developer Portal ของคุณ จำเป็นเฉพาะเมื่อคุณส่งการแจ้งเตือนพุชไปยังเบราว์เซอร์ Safari
defaultNotificationTitle: 'Pushwoosh', // กำหนดชื่อเรื่องเริ่มต้นสำหรับการแจ้งเตือนพุช
defaultNotificationImage: 'https://yoursite.com/img/logo-medium.png', // URL ไปยังรูปภาพการแจ้งเตือนที่กำหนดเอง
autoSubscribe: false, // หรือ true หากเป็น true จะแสดงพรอมต์ให้ผู้ใช้สมัครรับการแจ้งเตือนพุชเมื่อ SDK เริ่มต้นทำงาน
subscribeWidget: {
enable: true
},
userId: 'user_id', // ไม่บังคับ, กำหนด ID ผู้ใช้ที่กำหนดเอง
tags: {
'Name': 'John Smith' // ไม่บังคับ, กำหนดแท็กที่กำหนดเอง
}
}]);
</script>

การกำหนดค่า

Anchor link to

เพื่อให้การติดตั้งการแจ้งเตือนพุชในเว็บไซต์ของคุณเสร็จสมบูรณ์ คุณต้องกำหนดค่าแพลตฟอร์มเว็บใน Pushwoosh Control Panel ของคุณโดยทำตามคำแนะนำทีละขั้นตอนของเรา:

การลงทะเบียน service worker ในขอบเขตที่แตกต่างกัน

Anchor link to

ในบางครั้งคุณไม่สามารถวางไฟล์ service worker ไว้ในไดเรกทอรีรากของเว็บไซต์ได้ แต่ต้องวางไว้ในไดเรกทอรีย่อย

ในกรณีนี้ ให้แก้ไขการกำหนดค่า (ขั้นตอนที่ 4.3) โดยเพิ่มพารามิเตอร์

serviceWorkerUrl: “/push-notifications/pushwoosh-service-worker.js”

โดยที่ /push-notifications/pushwoosh-service-worker.js คือพาธไปยังไฟล์ pushwoosh-service-worker.js

ตัวจัดการอีเวนต์ (Event handlers)

Anchor link to

ใน Pushwoosh Web SDK 3.0 คุณสามารถสมัครรับอีเวนต์บางอย่างเพื่อติดตาม**,** หรือยกเลิกการสมัครรับอีเวนต์หากไม่ต้องการติดตามอีกต่อไป

หากต้องการติดตามการโหลดของ Web SDK 3.0 ให้เรียกใช้อีเวนต์ onLoad ดังนี้:

// Load Event
Pushwoosh.push(['onLoad', (api) => {
console.log('Pushwoosh load!');
}]);

หากต้องการติดตามการเริ่มต้นทำงานของ Web SDK ที่ถูกต้อง ให้เรียกใช้อีเวนต์ onReady:

// Ready Event
Pushwoosh.push((api) => {
console.log('Pushwoosh ready!');
});

หากต้องการสมัครรับหรือยกเลิกการสมัครรับอีเวนต์ใดๆ ของ SDK ให้ใช้ตัวจัดการ (handler) หลังจากที่ SDK โหลดเสร็จ:

Pushwoosh.push(['onLoad', (api) => {
function onEventNameHandler() {
console.log('Triggered event: event-name!');
}
// To subscribe to an event:
Pushwoosh.addEventHandler('event-name', onEventNameHandler)
// To unsubscribe from an event:
Pushwoosh.removeEventHandler('event-name', onEventNameHandler)
}]);

อีเวนต์ของ SDK

Anchor link to

อีเวนต์ Subscribe

Anchor link to

ทำงานหลังจากที่ผู้ใช้ตกลงที่จะรับการแจ้งเตือนพุช

Pushwoosh.push(['onLoad', (api) => {
Pushwoosh.addEventHandler('subscribe', (payload) => {
console.log('Triggered event: subscribe');
});
}]);

อีเวนต์ Unsubscribe

Anchor link to

ทำงานหลังจากที่อุปกรณ์ถูกยกเลิกการลงทะเบียนจากการแจ้งเตือน

Pushwoosh.push(['onLoad', (api) => {
Pushwoosh.addEventHandler('unsubscribe', (payload) => {
console.log('Triggered event: unsubscribe');
});
}]);

อีเวนต์วิดเจ็ตการสมัครรับข้อมูล

Anchor link to

ติดตามการแสดงผลของวิดเจ็ตพรอมต์การสมัครรับข้อมูล (Subscription Prompt)

Pushwoosh.push(['onLoad', (api) => {
// Executed on displaying of the Subscription Prompt widget
Pushwoosh.addEventHandler('show-subscription-widget', (payload) => {
console.log('Triggered event: show-subscription-widget');
});
// Executed on hiding of the Subscription Prompt widget
Pushwoosh.addEventHandler('hide-subscription-widget', (payload) => {
console.log('Triggered event: hide-subscription-widget');
});
}]);

อีเวนต์ไดอะล็อกการอนุญาตการแจ้งเตือน

Anchor link to

ติดตามการแสดงผลของไดอะล็อกการสมัครรับข้อมูลแบบเนทีฟ

Pushwoosh.push(['onLoad', function (api) {
// Executed on permission dialog displaying
Pushwoosh.addEventHandler('show-notification-permission-dialog', (payload) => {
console.log('Triggered event: show-notification-permission-dialog');
});
// Executed on hiding the permission dialog with one of three possible statuses:
// 1. default - the dialog is closed
// 2. granted - permission is granted
// 3. denied - permission is denied
Pushwoosh.addEventHandler('hide-notification-permission-dialog', (payload) => {
console.log('Triggered event: hide-notification-permission-dialog', payload.permission);
});
}]);

อีเวนต์การอนุญาต

Anchor link to

ตรวจสอบสถานะการอนุญาตการแจ้งเตือนพุชเมื่อ SDK เริ่มต้นทำงาน; ติดตามการอัปเดตสถานะนี้ทุกครั้งที่เกิดขึ้น

Pushwoosh.push(['onLoad', (api) => {
// Executed during the SDK initialization if 'autoSubscribe: false' or/and if a user ignores a push notification prompt.
Pushwoosh.addEventHandler('permission-default', (payload) => {
console.log('Triggered event: permission-default');
});
// Executed during the SDK initialization if notifications are blocked or once a user blocks push notifications.
Pushwoosh.addEventHandler('permission-denied', (payload) => {
console.log('Triggered event: permission-denied');
});
// Executed during the SDK initialization if notifications are allowed or once a user allows push notifications.
Pushwoosh.addEventHandler('permission-granted', (payload) => {
console.log('Triggered event: permission-granted');
});
}]);

อีเวนต์การรับพุช

Anchor link to

ติดตามการส่งพุชไปยังอุปกรณ์

Pushwoosh.push(['onLoad', (api) => {
// Executed when a push notification is displayed.
Pushwoosh.addEventHandler('receive-push', (payload) => {
console.log('Triggered event: receive-push', payload.notification);
});
}]);

อีเวนต์การแจ้งเตือน

Anchor link to

ติดตามว่าการแจ้งเตือนพุชถูกเปิดหรือปิดโดยผู้ใช้

Pushwoosh.push(['onLoad', (api) => {
// Executed when a user clicks on notification.
Pushwoosh.addEventHandler('open-notification', (payload) => {
console.log('Triggered event: open-notification', payload.notification);
});
// Executed when a user closes a push notification.
Pushwoosh.addEventHandler('hide-notification', (payload) => {
console.log('Triggered event: hide-notification', payload.notification);
});
}]);

อีเวนต์ Inbox

Anchor link to

ติดตามการแจ้งเตือนที่ส่งไปยัง Inbox

Pushwoosh.push(['onLoad', (api) => {
// Executed by ServiceWorker after the Inbox Message is received and saved to indexedDB.
Pushwoosh.addEventHandler('receive-inbox-message', (payload) => {
console.log('Triggered event: receive-inbox-message', payload.message);
});
// Executed after the Inbox is updated automatically while the page is loading.
Pushwoosh.addEventHandler('update-inbox-messages', (payload) => {
console.log('Triggered event: receive-inbox-message', payload.messages);
});
}]);

อีเวนต์ป๊อปอัปการสมัครรับข้อมูลแบบกำหนดเอง

Anchor link to

สำหรับรายละเอียดเกี่ยวกับการจัดการอีเวนต์ป๊อปอัปการสมัครรับข้อมูลแบบกำหนดเอง โปรดดูที่ คู่มืออีเวนต์ป๊อปอัปการสมัครรับข้อมูลแบบกำหนดเอง

หลังจากที่ Web Push SDK เริ่มต้นทำงานแล้ว คุณสามารถเรียกใช้ Pushwoosh API ต่อไปนี้ได้ เมธอดทั้งหมดจะส่งคืนอ็อบเจกต์ Promise

Pushwoosh.push((api) => {
// ตั้งค่าแท็กสำหรับผู้ใช้
api.setTags({
'Tag Name 1': 'value1',
'Tag Name 2': 'value2'
});
// รับแท็กสำหรับผู้ใช้จากเซิร์ฟเวอร์
api.getTags();
// ลงทะเบียน ID ผู้ใช้
api.registerUser('myUserID');
// ลงทะเบียนอีเมลผู้ใช้
api.registerEmail('<user_email>');
// โพสต์อีเวนต์
api.postEvent('myEventName', {attributeName: 'attributeValue'});
//ยกเลิกการลงทะเบียนจากการแจ้งเตือน
api.unregisterDevice();
});

ตัวอย่างการส่งแท็กไปยัง Pushwoosh:

Pushwoosh.push((api) => {
var myCustomTags = {
'Tag 1': 123,
'Tag 2': 'some string'
};
api.setTags(myCustomTags)
.then((res) => {
var skipped = res && res.skipped || [];
if (!skipped.length) {
console.log('success');
}
else {
console.warn('skipped tags:', skipped);
}
})
.catch((err) => {
console.error('setTags error:', err);
});
});

เพิ่มค่าแท็ก

Anchor link to

หากต้องการ เพิ่มค่า ของแท็กประเภทตัวเลข (Number Tag) ให้ใช้พารามิเตอร์ operation ที่มีค่าเป็น ‘increment’ ดังนี้:

Pushwoosh.push((api) => {
api.setTags({
'Tag 1': {
operation: 'increment',
value: 1
}
})
});

ต่อท้ายค่าแท็ก

Anchor link to

หากต้องการ ต่อท้ายค่าใหม่ ไปยังแท็กประเภทรายการ (List Tag) ที่มีอยู่ ให้ใช้พารามิเตอร์ operation ที่มีค่าเป็น ‘append’ ดังนี้:

Pushwoosh.push((api) => {
api.setTags({
'Tag 3': {
operation: 'append',
value: ['Value3']
}
})
});

ลบค่าแท็ก

Anchor link to

หากต้องการ ลบค่า ออกจากแท็กประเภทรายการ (List Tag) ให้ใช้พารามิเตอร์ operation ที่มีค่าเป็น ‘remove’ ดังนี้:

Pushwoosh.push((api) =>{
api.setTags({
'Tag 3': {
operation: 'remove',
value: ['Value2']
}
})
});

เมธอดสาธารณะ

Anchor link to

Pushwoosh.subscribe()

เมธอดนี้ใช้เพื่อขออนุญาตจากผู้ใช้สำหรับการแจ้งเตือนพุช หากผู้ใช้สมัครรับข้อมูลอยู่แล้ว เมธอดจะหยุดทำงาน

หากผู้ใช้ยังไม่ได้สมัครรับการแจ้งเตือนพุช:

1. จะมีการขออนุญาตสำหรับการแจ้งเตือนพุช

ไดอะล็อกขออนุญาตส่งการแจ้งเตือนพุช

2. หากผู้ใช้อนุญาตการแจ้งเตือน อีเวนต์ onSubscribe จะถูกเรียกใช้งาน

Pushwoosh.subscribe() จะทำงานโดยอัตโนมัติหากมีการตั้งค่า autoSubscribe: true ระหว่างการเริ่มต้น SDK

เรียกใช้เมธอดนี้หากคุณเลือกที่จะแจ้งให้ผู้ใช้สมัครรับการแจ้งเตือนพุชด้วยตนเองโดยใช้พารามิเตอร์ autoSubscribe: false ระหว่างการเริ่มต้น:

<button onclick="Pushwoosh.subscribe()">Subscribe</button>
<script>
Pushwoosh.push(['onSubscribe', (api) => {
console.log('User successfully subscribed');
}]);
</script>

Pushwoosh.unsubscribe()

  1. เมธอด /unregisterDevice จะทำงาน
  2. อีเวนต์ onUnsubscribe จะถูกเรียกใช้งาน
<button onclick="Pushwoosh.unsubscribe()">Unsubscribe</button>
<script type="text/javascript">
Pushwoosh.push(['onUnsubscribe', (api) => {
console.log('User successfully unsubscribed');
}]);
</script>

Pushwoosh.isSubscribed()

ตรวจสอบว่าผู้ใช้สมัครรับข้อมูลอยู่หรือไม่ และส่งคืนค่าสถานะเป็น true/false

Pushwoosh.isSubscribed().then((isSubscribed) => {
console.log('isSubscribed', isSubscribed);
});

Pushwoosh.getHWID()

ส่งคืนค่า Pushwoosh HWID

Pushwoosh.getHWID().then((hwid) => {
console.log('hwid:', hwid);
});

Pushwoosh.getPushToken()

ส่งคืนค่า push token หากมี

Pushwoosh.getPushToken().then((pushToken) => {
console.log('pushToken:', pushToken);
});

Pushwoosh.getUserId()

ส่งคืน ID ผู้ใช้ (User ID) หากมี

Pushwoosh.getUserId().then((userId) => {
console.log('userId:', userId);
});

Pushwoosh.getParams()

ส่งคืนรายการพารามิเตอร์ต่อไปนี้:

Pushwoosh.getParams().then((params) => {
params = params || {};
var hwid = params.hwid;
var pushToken = params.pushToken;
var userId = params.userId;
});

Pushwoosh.isAvailableNotifications()

ตรวจสอบว่าเบราว์เซอร์รองรับ Pushwoosh WebSDK 3.0 หรือไม่ ส่งคืนค่าเป็น ‘true’ หรือ ‘false’

Pushwoosh.isAvailableNotifications() // true/false

เมธอด InboxMessages

Anchor link to

messagesWithNoActionPerformedCount(): Promise<number>

ส่งคืนจำนวนข้อความที่เปิดแล้ว

Pushwoosh.pwinbox.messagesWithNoActionPerformedCount()
.then((count) => {
console.log(`${count} messages opened`);
});

unreadMessagesCount()

ส่งคืนจำนวนข้อความที่ยังไม่ได้อ่าน

Pushwoosh.pwinbox.unreadMessagesCount()
.then((count) => {
console.log(`${count} messages unread`);
});

messagesCount(): Promise<number>

ส่งคืนจำนวนข้อความทั้งหมด

Pushwoosh.pwinbox.messagesCount()
.then((count) => {
console.log(`${count} messages`);
});

loadMessages(): Promise<Array>

โหลดรายการข้อความที่ยังไม่ถูกลบ

Pushwoosh.pwinbox.loadMessages()
.then(() => {
console.log('Messages have been loaded');
});

readMessagesWithCodes(codes: Array<string>): Promise<void>

ทำเครื่องหมายข้อความว่าอ่านแล้วโดยใช้ Inbox_Ids

Pushwoosh.pwinbox.readMessagesWithCodes(codes)
.then(() => {
console.log('Messages have been read');
});

performActionForMessageWithCode(code: string): Promise<void>

ดำเนินการตามที่กำหนดไว้กับข้อความและทำเครื่องหมายข้อความว่าอ่านแล้ว

Pushwoosh.pwinbox.performActionForMessageWithCode(code)
.then(() => {
console.log('Action has been performed');
});

deleteMessagesWithCodes(codes: Array<string>): Promise<void>

ทำเครื่องหมายข้อความว่าถูกลบแล้ว

Pushwoosh.pwinbox.deleteMessagesWithCodes([code])
.then(() => {
console.log('Messages have been deleted');
});

syncMessages(): Promise<void>

ซิงโครไนซ์ข้อความกับเซิร์ฟเวอร์

Pushwoosh.pwinbox.syncMessages()
.then(() => {
console.log('Messages have been synchronized');
});

การรองรับ Progressive Web App

Anchor link to

ในการผสาน Pushwoosh เข้ากับ Progressive Web Application (PWA) ของคุณ ให้ทำตามขั้นตอนที่อธิบายไว้ด้านล่าง

1. คัดลอกพาธไปยังไฟล์ Service Worker ของคุณ:

if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js') // <- url ของ service worker ของคุณ
});
}

จากนั้น ใช้พารามิเตอร์ serviceWorkerUrl ขณะเริ่มต้นการทำงาน WebSDK ดังนี้:

var Pushwoosh = Pushwoosh || [];
Pushwoosh.push(['init', {
logLevel: 'error',
applicationCode: 'XXXXX-XXXXX',
safariWebsitePushID: 'web.com.example.domain',
defaultNotificationTitle: 'Pushwoosh',
defaultNotificationImage: 'https://yoursite.com/img/logo-medium.png',
serviceWorkerUrl: '/service-worker.js', // <- url ของ service worker ของคุณ
}]);

WebSDK จะไม่ลงทะเบียน Service Worker ใหม่ทันที; Service Worker จะถูกลงทะเบียนเมื่อจำเป็น:

  • เมื่ออุปกรณ์ได้รับ push token (ในการลงทะเบียนอุปกรณ์หรือการสมัครรับข้อมูลอีกครั้ง)
  • เมื่อ push token ถูกลบ (ในการลบอุปกรณ์ออกจากฐานผู้ใช้)

ช่วยให้การโหลดหน้าเว็บของคุณเร็วขึ้นโดยการลดจำนวนการร้องขอไปยังเซิร์ฟเวอร์

เบราว์เซอร์ไม่อนุญาตให้ลงทะเบียน Service Worker สองตัวที่แตกต่างกันในเวลาเดียวกัน (อ่านเพิ่มเติม: https://github.com/w3c/ServiceWorker/issues/921) ดังนั้นเพื่อให้ PWA ของคุณทำงานได้อย่างถูกต้อง ควรลงทะเบียน Service Worker ร่วมกันสำหรับ codebase ของคุณและ codebase ของ Pushwoosh

2. เพิ่มสตริงต่อไปนี้ลงใน Service Worker ของคุณ (ที่จุดเริ่มต้นหรือตอนท้ายก็ได้ ไม่มีความแตกต่าง):

importScripts('https://cdn.pushwoosh.com/webpush/v3/pushwoosh-service-worker.js' + self.location.search);

ด้วยวิธีนี้ คุณจะเปิดใช้งานการรับและประมวลผลการแจ้งเตือนพุชที่ส่งผ่านบริการของ Pushwoosh สำหรับ Service Worker ของคุณ

การติดตั้งจาก Google Tag Manager

Anchor link to

ใช้โค้ดต่อไปนี้ใน Google Tag Manager ของคุณเพื่อเริ่มต้นการทำงาน Pushwoosh SDK สร้าง Custom HTML Tag และวางโค้ดด้านล่าง ตรวจสอบให้แน่ใจว่าได้เปลี่ยน Pushwoosh Application Code, Safari Website ID และ URL รูปภาพการแจ้งเตือนเริ่มต้นของคุณ
นอกจากนี้ให้ตั้งค่าลำดับความสำคัญของ Tag Firing ให้สูง (เช่น 100) และทริกเกอร์บน All Pages ดูภาพหน้าจอด้านล่าง

<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', {
logLevel: 'error',
applicationCode: 'XXXXX-XXXXX',
safariWebsitePushID: 'web.com.example.domain',
defaultNotificationTitle: 'Pushwoosh',
defaultNotificationImage: 'https://yoursite.com/img/logo-medium.png',
autoSubscribe: true,
subscribeWidget: {
enable: false
},
userId: 'user_id'
}]);
</script>
การกำหนดค่าแท็กใน Google Tag Manager