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/types/phonegap-plugin-push/index.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 | const push = PushNotification.init({
|
13 | android: {
|
14 | },
|
15 | ios: {
|
16 | alert: "true",
|
17 | badge: true,
|
18 | sound: 'false'
|
19 | },
|
20 | windows: {}
|
21 | });
|
22 |
|
23 | push.on('registration', (data) => {
|
24 | console.log(data.registrationId);
|
25 | });
|
26 |
|
27 | push.on('notification', (data) => {
|
28 | console.log(data.message);
|
29 | console.log(data.title);
|
30 | console.log(data.count);
|
31 | console.log(data.sound);
|
32 | console.log(data.image);
|
33 | console.log(data.additionalData);
|
34 | });
|
35 |
|
36 | push.on('error', (e) => {
|
37 | console.log(e.message);
|
38 | });
|
39 | ```
|
40 |
|
41 | 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:
|
42 |
|
43 | ```typescript
|
44 | module my.custom {
|
45 | export interface NotificationEventResponse extends PhonegapPluginPush.NotificationEventResponse {
|
46 | additionalData: NotificationEventAdditionalData;
|
47 | }
|
48 |
|
49 | export interface NotificationEventAdditionalData extends PhonegapPluginPush.NotificationEventAdditionalData {
|
50 | bacon?: boolean;
|
51 | }
|
52 | }
|
53 |
|
54 | push.on('notification', (data: my.custom.NotificationEventResponse) => {
|
55 | //standard attributes
|
56 | console.log(data.message);
|
57 | console.log(data.title);
|
58 | console.log(data.count);
|
59 | console.log(data.sound);
|
60 | console.log(data.image);
|
61 | console.log(data.additionalData);
|
62 |
|
63 | //custom attributes
|
64 | console.log(data.additionalData.bacon);
|
65 | });
|
66 | ```
|
67 |
|
68 | ## Outdated definitions
|
69 |
|
70 | Is our definition file at DefinitelyTyped outdated? Is there any improvements that could be done?
|
71 | We welcome any contribution, and they should be done through issues created [there](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/new).
|