# การอ้างอิง API ของปลั๊กอิน Cordova

```txt title="example"
var pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");

// Should be called before pushwoosh.onDeviceReady
document.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

```javascript
PushNotification.prototype.onDeviceReady = function( config )
```

_\[android, ios, wp8, windows]_\
เริ่มต้นปลั๊กอิน Pushwoosh และทริกเกอร์ข้อความพุชเริ่มต้น ควรเรียกใช้ทุกครั้งที่เปิดแอป

`config.appid` – รหัสแอปพลิเคชัน Pushwoosh

`config.serviceName` – ชื่อบริการ MPNS สำหรับแพลตฟอร์ม wp8

```txt title="example"
// 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

```javascript
PushNotification.prototype.registerDevice = function( success, fail )
```

_\[android, ios, wp8, windows]_\
ลงทะเบียนอุปกรณ์สำหรับการแจ้งเตือนแบบพุชและรับ Push Token

`success` – callback เมื่อสำเร็จ Push token จะถูกส่งเป็นพารามิเตอร์ “status.pushToken” ไปยัง callback นี้

`fail` – callback เมื่อเกิดข้อผิดพลาด

```txt title="example"
pushwoosh.registerDevice(
    function(status) {
        alert("Registered with push token: " + status.pushToken);
    },
    function(error) {
        alert("Failed to register: " +  error);
    }
);
```

## unregisterDevice

```javascript
PushNotification.prototype.unregisterDevice = function(	success, fail	)
```

_\[android, ios, wp8, windows]_\
ยกเลิกการลงทะเบียนอุปกรณ์จากการรับการแจ้งเตือนแบบพุช

`success` – callback เมื่อสำเร็จ

`fail` – callback เมื่อเกิดข้อผิดพลาด

## setTags

```javascript
PushNotification.prototype.setTags = function(	config, success, fail	)
```

_\[android, ios, wp8, windows]_\
ตั้งค่าแท็กสำหรับอุปกรณ์

**พารามิเตอร์**

`config` – อ็อบเจกต์ที่มีแท็กอุปกรณ์ที่กำหนดเอง

`success` – callback เมื่อสำเร็จ Push token จะถูกส่งเป็นพารามิเตอร์ “status.pushToken” ไปยัง callback นี้

`fail` – callback เมื่อเกิดข้อผิดพลาด

```txt title="example"
// sets tags: “deviceName” with value “hello” and “deviceId” with value 10
pushwoosh.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

```javascript
PushNotification.prototype.getTags = function(	success, fail	)
```

_\[android, ios, wp8, windows]_\
ส่งคืนแท็กสำหรับอุปกรณ์รวมถึงแท็กเริ่มต้น

`success` – callback เมื่อสำเร็จ รับแท็กเป็นพารามิเตอร์

`fail` – callback เมื่อเกิดข้อผิดพลาด

```javascript
pushwoosh.getTags(
    function(tags) {
        console.warn('tags for the device: ' + JSON.stringify(tags));
    },
    function(error) {
        console.warn('get tags error: ' + JSON.stringify(error));
    }
);
```

## getPushToken

```javascript
PushNotification.prototype.getPushToken = function(	success	)
```

_\[android, ios, wp8, windows]_\
ส่งคืน push token หากมีอยู่ โปรดทราบว่า token จะมาใน callback ของฟังก์ชัน registerDevice ด้วย

`success` – callback เมื่อสำเร็จ

```javascript
pushwoosh.getPushToken(
    function(token) {
        console.warn('push token: ' + token);
    }
);
```

## getPushwooshHWID

```javascript
PushNotification.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

```javascript
PushNotification.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

```javascript
PushNotification.prototype.setApplicationIconBadgeNumber = function(	badgeNumber	)
```

_\[android, ios]_\
ตั้งค่าหมายเลขป้ายไอคอนแอปพลิเคชัน

`badgeNumber` – หมายเลขป้ายไอคอน

## getApplicationIconBadgeNumber

```javascript
PushNotification.prototype.getApplicationIconBadgeNumber = function(	callback	)
```

_\[android, ios]_\
ส่งคืนหมายเลขป้ายไอคอนแอปพลิเคชัน

`callback` – callback เมื่อสำเร็จ

```
pushwoosh.getApplicationIconBadgeNumber(function(badge){ alert(badge);} );
```

## addToApplicationIconBadgeNumber

```javascript
PushNotification.prototype.addToApplicationIconBadgeNumber = function( badgeNumber )
```

_\[android, ios]_\
เพิ่มค่าให้กับป้ายไอคอนแอปพลิเคชัน

`badgeNumber` — หมายเลขป้ายไอคอนที่เพิ่มขึ้น

## getLaunchNotification

```javascript
PushNotification.prototype.getLaunchNotification = function(	callback	)
```

_\[android, ios]_\
ส่งคืน payload ของการแจ้งเตือนแบบพุชหากแอปถูกเริ่มจากการตอบสนองต่อการแจ้งเตือนแบบพุช หรือ null

`callback` – callback เมื่อสำเร็จ

## clearLaunchNotification

```javascript
PushNotification.prototype.clearLaunchNotification = function(	callback	)
```

_\[android, ios]_\
ล้างการแจ้งเตือนการเปิดแอป getLaunchNotification() จะส่งคืน null หลังจากการเรียกนี้

## setUserId

```javascript
PushNotification.prototype.setUserId = function(	userId	)
```

_\[android, ios]_\
ตั้งค่าตัวระบุผู้ใช้– Facebook ID, ชื่อผู้ใช้, อีเมล, หรือ ID ผู้ใช้อื่นๆ ซึ่งช่วยให้ข้อมูลและอีเวนต์สามารถจับคู่กันได้ในอุปกรณ์ต่างๆ ของผู้ใช้

`userId` – ตัวระบุผู้ใช้แบบสตริง

## postEvent

```javascript
PushNotification.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

```javascript
PushNotification.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

```javascript
PushNotification.prototype.clearLocalNotification = function()
```

_\[android]_\
ล้างการแจ้งเตือนในเครื่องที่รอดำเนินการทั้งหมดที่สร้างโดย createLocalNotification

## clearNotificationCenter

```javascript
PushNotification.prototype.clearNotificationCenter = function()
```

_\[android]_\
ล้างการแจ้งเตือนทั้งหมดที่แสดงใน Android Notification Center

## setMultiNotificationMode

```javascript
PushNotification.prototype.setMultiNotificationMode = function( success, fail )
```

_\[android]_\
อนุญาตให้แสดงการแจ้งเตือนหลายรายการใน Android Notification Center

## setSingleNotificationMode

```javascript
PushNotification.prototype.setSingleNotificationMode = function(	success,
fail	)
```

_\[android]_\
อนุญาตให้แสดงเฉพาะการแจ้งเตือนล่าสุดใน Android Notification Center

## setSoundType

```javascript
PushNotification.prototype.setSoundType = function( type, success, fail )
```

_\[android]_\
ตั้งค่าเสียงเริ่มต้นสำหรับพุชที่เข้ามา

`type` – ประเภทเสียง (0 – ค่าเริ่มต้น, 1 – ไม่มีเสียง, 2 – เสมอ)

## setVibrateType

```javascript
PushNotification.prototype.setVibrateType = function(type, success, fail )
```

_\[android]_\
ตั้งค่าโหมดการสั่นเริ่มต้นสำหรับพุชที่เข้ามา

`type` – ประเภทการสั่น (0 – ค่าเริ่มต้น, 1 – ไม่มีการสั่น, 2 – เสมอ)

## setLightScreenOnNotification

```javascript
PushNotification.prototype.setLightScreenOnNotification = function( on, success, fail )
```

_\[android]_\
เปิดหน้าจอเมื่อมีการแจ้งเตือนเข้ามา

`on` – เปิด/ปิดการปลดล็อกหน้าจอ (ปิดใช้งานโดยค่าเริ่มต้น)

## setEnableLED

```javascript
PushNotification.prototype.setEnableLED = function( on, success, fail )
```

_\[android]_\
เปิดใช้งานไฟ LED กระพริบเมื่อมีการแจ้งเตือนเข้ามาและหน้าจอปิดอยู่

`on` – เปิด/ปิดการกระพริบของ LED (ปิดใช้งานโดยค่าเริ่มต้น)

## setColorLED

```javascript
PushNotification.prototype.setColorLED = function( color, success, fail )
```

_\[android]_\
ตั้งค่าสีของไฟ LED ใช้กับ [setEnableLED](#setenableled)

`color` – สีของ LED ในรูปแบบจำนวนเต็ม ARGB

## getPushHistory

```javascript
PushNotification.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

```javascript
PushNotification.prototype.clearPushHistory = function()
```

_\[android]_\
ล้างประวัติการพุช

## cancelAllLocalNotifications

```javascript
PushNotification.prototype.cancelAllLocalNotifications = function( callback )
```

_\[ios]_\
ล้างการแจ้งเตือนในเครื่องทั้งหมดออกจากศูนย์การแจ้งเตือน

## presentInboxUI

_\[android, ios]_\
เปิดหน้าจอ [Inbox](/th/developer/guides/message-inbox/mobile-message-inbox)

```javascript
PushNotification.prototype.presentInboxUI = function()
```

## setCommunicationEnabled

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

```javascript
PushNotification.prototype.setCommunicationEnabled = function(enable, success, fail)
```

## removeAllDeviceData

ลบข้อมูลทั้งหมดเกี่ยวกับอุปกรณ์

```javascript
PushNotification.prototype.removeAllDeviceData = function()
```

## push-receive

_\[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));
		}
	}
);
```

## การแจ้งเตือนในเบื้องหน้า

โดยค่าเริ่มต้น ปลั๊กอิน Pushwoosh จะไม่แสดงการแจ้งเตือนในเบื้องหน้าและจะทริกเกอร์อีเวนต์ `push-receive` โดยอัตโนมัติ ดู [คู่มือการปรับแต่งปลั๊กอิน](/th/developer/pushwoosh-sdk/cross-platform-frameworks/cordova/customizing-cordova-plugin/) สำหรับการควบคุมพฤติกรรมนี้

### push-notification

_\[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](#push-receive)

### additionalAuthorizationOptions

_\[ios เท่านั้น]_\
ให้ _ตัวเลือก_ เพิ่มเติมสำหรับ [การอนุญาตการแจ้งเตือน](https://developer.apple.com/documentation/usernotifications/unauthorizationoptions?language=objc) ควรเรียกใช้ก่อนเรียก **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. 
});
```