1 | # Typescript definition file
|
2 |
|
3 | For those of you who use typescript, we're glad to say that we have the complete definition file available at [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped).
|
4 | Search for `phonegap-plugin-push` there, or simply grab it directly [here](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/phonegap-plugin-push/phonegap-plugin-push.d.ts).
|
5 |
|
6 | ## Example usage
|
7 |
|
8 | All objects will be understood as having a defined type, including init options and eventHandler parameters.
|
9 | All available attributes and properties will have autocomplete support and type checkings.
|
10 |
|
11 | ```typescript
|
12 | let push = PushNotification.init({
|
13 | android: {
|
14 | senderID: "12345679"
|
15 | },
|
16 | ios: {
|
17 | alert: "true",
|
18 | badge: true,
|
19 | sound: 'false'
|
20 | },
|
21 | windows: {}
|
22 | });
|
23 |
|
24 | push.on('registration', (data) => {
|
25 | console.log(data.registrationId);
|
26 | });
|
27 |
|
28 | push.on('notification', (data) => {
|
29 | console.log(data.message);
|
30 | console.log(data.title);
|
31 | console.log(data.count);
|
32 | console.log(data.sound);
|
33 | console.log(data.image);
|
34 | console.log(data.additionalData);
|
35 | });
|
36 |
|
37 | push.on('error', (e) => {
|
38 | console.log(e.message);
|
39 | });
|
40 | ```
|
41 |
|
42 | If you have custom attributes being sent from the server on the payload, you can define them on a custom interface extending the standard one:
|
43 |
|
44 | ```typescript
|
45 | module my.custom {
|
46 | export interface NotificationEventResponse extends PhonegapPluginPush.NotificationEventResponse {
|
47 | additionalData: NotificationEventAdditionalData;
|
48 | }
|
49 |
|
50 | export interface NotificationEventAdditionalData extends PhonegapPluginPush.NotificationEventAdditionalData {
|
51 | bacon?: boolean;
|
52 | }
|
53 | }
|
54 |
|
55 | push.on('notification', (data: my.custom.NotificationEventResponse) => {
|
56 | //standard attributes
|
57 | console.log(data.message);
|
58 | console.log(data.title);
|
59 | console.log(data.count);
|
60 | console.log(data.sound);
|
61 | console.log(data.image);
|
62 | console.log(data.additionalData);
|
63 |
|
64 | //custom attributes
|
65 | console.log(data.additionalData.bacon);
|
66 | });
|
67 | ```
|
68 |
|
69 | ## Outdated definitions
|
70 |
|
71 | Is our definition file at DefinitelyTyped outdated? Is there any improvements that could be done?
|
72 | We welcome any contribution, and they should be done through issues created [there](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/new). |
\ | No newline at end of file |