การอ้างอิง API ของปลั๊กอิน Cordova
var pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");
// Should be called before pushwoosh.onDeviceReadydocument.addEventListener('push-notification', function(event) { var notification = event.notification; // handle push open here});
// Initialize Pushwoosh. This will trigger all pending push notifications on start.pushwoosh.onDeviceReady({ appid: "XXXXX-XXXXX", serviceName: "XXXX"});
pushwoosh.registerDevice( function(status) { var pushToken = status.pushToken; // handle successful registration here }, function(status) { // handle registration error here });onDeviceReady
Anchor link toPushNotification.prototype.onDeviceReady = function( config )[android, ios, wp8, windows]
เริ่มต้นปลั๊กอิน Pushwoosh และทริกเกอร์ข้อความพุชเริ่มต้น ควรเรียกใช้ทุกครั้งที่เปิดแอป
config.appid – รหัสแอปพลิเคชัน Pushwoosh
config.serviceName – ชื่อบริการ MPNS สำหรับแพลตฟอร์ม wp8
// initialize Pushwoosh with appid : "PUSHWOOSH_APP_ID", serviceName : "WINDOWS_PHONE_SERVICE". This will trigger all pending push notifications on start.pushwoosh.onDeviceReady({ appid : "XXXXX-XXXXX", serviceName: "XXXX"});registerDevice
Anchor link toPushNotification.prototype.registerDevice = function( success, fail )[android, ios, wp8, windows]
ลงทะเบียนอุปกรณ์สำหรับการแจ้งเตือนแบบพุชและรับ Push Token
success – callback เมื่อสำเร็จ Push token จะถูกส่งเป็นพารามิเตอร์ “status.pushToken” ไปยัง callback นี้
fail – callback เมื่อเกิดข้อผิดพลาด
pushwoosh.registerDevice( function(status) { alert("Registered with push token: " + status.pushToken); }, function(error) { alert("Failed to register: " + error); });unregisterDevice
Anchor link toPushNotification.prototype.unregisterDevice = function( success, fail )[android, ios, wp8, windows]
ยกเลิกการลงทะเบียนอุปกรณ์จากการรับการแจ้งเตือนแบบพุช
success – callback เมื่อสำเร็จ
fail – callback เมื่อเกิดข้อผิดพลาด
setTags
Anchor link toPushNotification.prototype.setTags = function( config, success, fail )[android, ios, wp8, windows]
ตั้งค่าแท็กสำหรับอุปกรณ์
พารามิเตอร์
config – อ็อบเจกต์ที่มีแท็กอุปกรณ์ที่กำหนดเอง
success – callback เมื่อสำเร็จ Push token จะถูกส่งเป็นพารามิเตอร์ “status.pushToken” ไปยัง callback นี้
fail – callback เมื่อเกิดข้อผิดพลาด
// sets tags: “deviceName” with value “hello” and “deviceId” with value 10pushwoosh.setTags({deviceName:"hello", deviceId:10}, function() { console.warn('setTags success'); }, function(error) { console.warn('setTags failed'); });
// sets list tags "MyTag" with values (array) "hello", "world"pushwoosh.setTags({"MyTag":["hello", "world"]});getTags
Anchor link toPushNotification.prototype.getTags = function( success, fail )[android, ios, wp8, windows]
ส่งคืนแท็กสำหรับอุปกรณ์รวมถึงแท็กเริ่มต้น
success – callback เมื่อสำเร็จ รับแท็กเป็นพารามิเตอร์
fail – callback เมื่อเกิดข้อผิดพลาด
pushwoosh.getTags( function(tags) { console.warn('tags for the device: ' + JSON.stringify(tags)); }, function(error) { console.warn('get tags error: ' + JSON.stringify(error)); });getPushToken
Anchor link toPushNotification.prototype.getPushToken = function( success )[android, ios, wp8, windows]
ส่งคืน push token หากมีอยู่ โปรดทราบว่า token จะมาใน callback ของฟังก์ชัน registerDevice ด้วย
success – callback เมื่อสำเร็จ
pushwoosh.getPushToken( function(token) { console.warn('push token: ' + token); });getPushwooshHWID
Anchor link toPushNotification.prototype.getPushwooshHWID = function( success )[android, ios, wp8, windows]
ส่งคืน Pushwoosh HWID ที่ใช้สำหรับการสื่อสารกับ Pushwoosh API
success – callback ของ getPushwooshHWID
pushwoosh.getPushwooshHWID( function(token) { console.warn('Pushwoosh HWID: ' + token); });getRemoteNotificationStatus
Anchor link toPushNotification.prototype.getRemoteNotificationStatus = function( callback, error )[android, ios]
ส่งคืนสถานะโดยละเอียดของสิทธิ์การแจ้งเตือนแบบพุช
callback – callback เมื่อสำเร็จ รับอ็อบเจกต์ที่มีคุณสมบัติดังต่อไปนี้:
{ "enabled" : notificaions enabled flag. "pushBadge" : badges permission granted. (iOS only) "pushAlert" : alert permission granted. (iOS only) "pushSound" : sound permission granted. (iOS only)}error — callback เมื่อเกิดข้อผิดพลาด
setApplicationIconBadgeNumber
Anchor link toPushNotification.prototype.setApplicationIconBadgeNumber = function( badgeNumber )[android, ios]
ตั้งค่าหมายเลขป้ายไอคอนแอปพลิเคชัน
badgeNumber – หมายเลขป้ายไอคอน
getApplicationIconBadgeNumber
Anchor link toPushNotification.prototype.getApplicationIconBadgeNumber = function( callback )[android, ios]
ส่งคืนหมายเลขป้ายไอคอนแอปพลิเคชัน
callback – callback เมื่อสำเร็จ
pushwoosh.getApplicationIconBadgeNumber(function(badge){ alert(badge);} );addToApplicationIconBadgeNumber
Anchor link toPushNotification.prototype.addToApplicationIconBadgeNumber = function( badgeNumber )[android, ios]
เพิ่มค่าให้กับป้ายไอคอนแอปพลิเคชัน
badgeNumber — หมายเลขป้ายไอคอนที่เพิ่มขึ้น
getLaunchNotification
Anchor link toPushNotification.prototype.getLaunchNotification = function( callback )[android, ios]
ส่งคืน payload ของการแจ้งเตือนแบบพุชหากแอปถูกเริ่มจากการตอบสนองต่อการแจ้งเตือนแบบพุช หรือ null
callback – callback เมื่อสำเร็จ
clearLaunchNotification
Anchor link toPushNotification.prototype.clearLaunchNotification = function( callback )[android, ios]
ล้างการแจ้งเตือนการเปิดแอป getLaunchNotification() จะส่งคืน null หลังจากการเรียกนี้
setUserId
Anchor link toPushNotification.prototype.setUserId = function( userId )[android, ios]
ตั้งค่าตัวระบุผู้ใช้– Facebook ID, ชื่อผู้ใช้, อีเมล, หรือ ID ผู้ใช้อื่นๆ ซึ่งช่วยให้ข้อมูลและอีเวนต์สามารถจับคู่กันได้ในอุปกรณ์ต่างๆ ของผู้ใช้
userId – ตัวระบุผู้ใช้แบบสตริง
postEvent
Anchor link toPushNotification.prototype.postEvent = function( event, attributes )[android, ios]
โพสต์อีเวนต์สำหรับ In-App Messages ซึ่งสามารถทริกเกอร์การแสดงข้อความ In-App ตามที่ระบุใน Pushwoosh Control Panel
event – อีเวนต์ที่จะทริกเกอร์
attributes – อ็อบเจกต์ที่มีแอตทริบิวต์อีเวนต์เพิ่มเติม
pushwoosh.setUserId("XXXXXX");pushwoosh.postEvent("buttonPressed", { "buttonNumber" : 4, "buttonLabel" : "banner" });createLocalNotification
Anchor link toPushNotification.prototype.createLocalNotification = function( config, success, fail )[android, ios]
ตั้งเวลาการแจ้งเตือนในเครื่อง
config.msg – ข้อความแจ้งเตือน
config.seconds – การหน่วงเวลาการแจ้งเตือนเป็นวินาที
config.userData – ข้อมูลเพิ่มเติมที่จะส่งในการแจ้งเตือน
success – callback เมื่อสำเร็จ
fail – callback เมื่อเกิดข้อผิดพลาด
pushwoosh.createLocalNotification({msg:"Your pumpkins are ready!", seconds:30, userData:{}})clearLocalNotification
Anchor link toPushNotification.prototype.clearLocalNotification = function()[android]
ล้างการแจ้งเตือนในเครื่องที่รอดำเนินการทั้งหมดที่สร้างโดย createLocalNotification
clearNotificationCenter
Anchor link toPushNotification.prototype.clearNotificationCenter = function()[android]
ล้างการแจ้งเตือนทั้งหมดที่แสดงใน Android Notification Center
setMultiNotificationMode
Anchor link toPushNotification.prototype.setMultiNotificationMode = function( success, fail )[android]
อนุญาตให้แสดงการแจ้งเตือนหลายรายการใน Android Notification Center
setSingleNotificationMode
Anchor link toPushNotification.prototype.setSingleNotificationMode = function( success,fail )[android]
อนุญาตให้แสดงเฉพาะการแจ้งเตือนล่าสุดใน Android Notification Center
setSoundType
Anchor link toPushNotification.prototype.setSoundType = function( type, success, fail )[android]
ตั้งค่าเสียงเริ่มต้นสำหรับพุชที่เข้ามา
type – ประเภทเสียง (0 – ค่าเริ่มต้น, 1 – ไม่มีเสียง, 2 – เสมอ)
setVibrateType
Anchor link toPushNotification.prototype.setVibrateType = function(type, success, fail )[android]
ตั้งค่าโหมดการสั่นเริ่มต้นสำหรับพุชที่เข้ามา
type – ประเภทการสั่น (0 – ค่าเริ่มต้น, 1 – ไม่มีการสั่น, 2 – เสมอ)
setLightScreenOnNotification
Anchor link toPushNotification.prototype.setLightScreenOnNotification = function( on, success, fail )[android]
เปิดหน้าจอเมื่อมีการแจ้งเตือนเข้ามา
on – เปิด/ปิดการปลดล็อกหน้าจอ (ปิดใช้งานโดยค่าเริ่มต้น)
setEnableLED
Anchor link toPushNotification.prototype.setEnableLED = function( on, success, fail )[android]
เปิดใช้งานไฟ LED กระพริบเมื่อมีการแจ้งเตือนเข้ามาและหน้าจอปิดอยู่
on – เปิด/ปิดการกระพริบของ LED (ปิดใช้งานโดยค่าเริ่มต้น)
setColorLED
Anchor link toPushNotification.prototype.setColorLED = function( color, success, fail )[android]
ตั้งค่าสีของไฟ LED ใช้กับ setEnableLED
color – สีของ LED ในรูปแบบจำนวนเต็ม ARGB
getPushHistory
Anchor link toPushNotification.prototype.getPushHistory = function( success )[android]
ส่งคืนอาร์เรย์ของการแจ้งเตือนแบบพุชที่ได้รับ
success – callback เมื่อสำเร็จ
pushwoosh.getPushHistory(function(pushHistory) { if(pushHistory.length == 0) alert("no push history"); else alert(JSON.stringify(pushHistory));});
pushwoosh.clearPushHistory();clearPushHistory
Anchor link toPushNotification.prototype.clearPushHistory = function()[android]
ล้างประวัติการพุช
cancelAllLocalNotifications
Anchor link toPushNotification.prototype.cancelAllLocalNotifications = function( callback )[ios]
ล้างการแจ้งเตือนในเครื่องทั้งหมดออกจากศูนย์การแจ้งเตือน
presentInboxUI
Anchor link to[android, ios]
เปิดหน้าจอ Inbox
PushNotification.prototype.presentInboxUI = function()setCommunicationEnabled
Anchor link toเมธอดแบบไบนารีที่เปิด/ปิดการสื่อสารทั้งหมดกับ Pushwoosh ค่าบูลีน false จะยกเลิกการสมัครรับการแจ้งเตือนแบบพุชของอุปกรณ์และหยุดการดาวน์โหลดข้อความในแอป ค่า true จะให้ผลตรงกันข้าม
PushNotification.prototype.setCommunicationEnabled = function(enable, success, fail)removeAllDeviceData
Anchor link toลบข้อมูลทั้งหมดเกี่ยวกับอุปกรณ์
PushNotification.prototype.removeAllDeviceData = function()push-receive
Anchor link to[android, ios]
อีเวนต์การรับการแจ้งเตือนแบบพุช จะถูกยิงเมื่อแอปพลิเคชันได้รับการแจ้งเตือนแบบพุชในเบื้องหน้าหรือเบื้องหลัง แอปพลิเคชันที่ปิดอยู่จะไม่ได้รับอีเวนต์นี้
คุณสมบัติของอีเวนต์
message – (string) ข้อความแจ้งเตือนแบบพุช
userdata – (object/array) ข้อมูลที่กำหนดเองของการแจ้งเตือนแบบพุช
onStart – (boolean) เป็นการแจ้งเตือนการเปิดแอปหรือไม่
foreground – (boolean) เป็นการแจ้งเตือนที่ได้รับในเบื้องหน้าหรือไม่
android – (object) payload การแจ้งเตือนเฉพาะของ Android
ios – (object) payload การแจ้งเตือนเฉพาะของ iOS
windows – (object) payload การแจ้งเตือนเฉพาะของ Windows
document.addEventListener('push-receive', function(event) { var userData = event.notification.userdata;
if (typeof(userData) != "undefined") { // handle custom notification data console.warn('user data: ' + JSON.stringify(userData)); } });การแจ้งเตือนในเบื้องหน้า
Anchor link toโดยค่าเริ่มต้น ปลั๊กอิน Pushwoosh จะไม่แสดงการแจ้งเตือนในเบื้องหน้าและจะทริกเกอร์อีเวนต์ push-receive โดยอัตโนมัติ ดู คู่มือการปรับแต่งปลั๊กอิน สำหรับการควบคุมพฤติกรรมนี้
push-notification
Anchor link to[android, ios, wp8, windows]
อีเวนต์การยอมรับการแจ้งเตือนแบบพุช จะถูกยิงเมื่อผู้ใช้แตะที่การแจ้งเตือนแบบพุช
document.addEventListener('push-notification', function(event) { var message = event.notification.message; var userData = event.notification.userdata;
if (typeof(userData) != "undefined") { console.warn('user data: ' + JSON.stringify(userData)); } });คุณสมบัติของอีเวนต์
เหมือนกับ push-receive
additionalAuthorizationOptions
Anchor link to[ios เท่านั้น]
ให้ ตัวเลือก เพิ่มเติมสำหรับ การอนุญาตการแจ้งเตือน ควรเรียกใช้ก่อนเรียก registerDevice
pushwoosh.additionalAuthorizationOptions({ "UNAuthorizationOptionCriticalAlert" : 1, "UNAuthorizationOptionProvisional": 0 // set 0 or don't specify the option if you don't want to add it to your app.});