UNPKG

18.5 kBMarkdownView Raw
1# API
2
3- [.init()](#pushnotificationinitoptions)
4- [.hasPermission() - Android & iOS only](#pushnotificationhaspermissionsuccesshandler---android--ios-only)
5- [push.on()](#pushonevent-callback)
6 - [push.on('registration')](#pushonregistration-callback)
7 - [push.on('notification')](#pushonnotification-callback)
8 - [push.on('error')](#pushonerror-callback)
9- [push.off()](#pushoffevent-callback)
10- [push.unregister()](#pushunregistersuccesshandler-errorhandler-topics)
11- [push.subscribe()](#pushsubscribetopic-successhandler-errorhandler)
12- [push.unsubscribe()](#pushunsubscribetopic-successhandler-errorhandler)
13- [push.setApplicationIconBadgeNumber() - iOS & Android only](#pushsetapplicationiconbadgenumbersuccesshandler-errorhandler-count---ios--android-only)
14- [push.getApplicationIconBadgeNumber() - iOS & Android only](#pushgetapplicationiconbadgenumbersuccesshandler-errorhandler---ios--android-only)
15- [push.finish() - iOS only](#pushfinishsuccesshandler-errorhandler-id---ios-only)
16- [push.clearAllNotifications() - iOS & Android only](#pushclearallnotificationssuccesshandler-errorhandler---ios--android-only)
17
18## PushNotification.init(options)
19
20Initializes the plugin on the native side.
21
22**Note:** like all plugins you must wait until you receive the [`deviceready`](https://cordova.apache.org/docs/en/5.4.0/cordova/events/events.deviceready.html) event before calling `PushNotification.init()`.
23
24**Note:** you will want to call `PushNotification.init()` each time your app starts. The remote push service can periodically reset your registration ID so this ensures you have the correct value.
25
26### Returns
27
28- Instance of `PushNotification`.
29
30### Parameters
31
32Parameter | Type | Default | Description
33--------- | ---- | ------- | -----------
34`options` | `Object` | `{}` | An object describing relevant specific options for all target platforms.
35
36All available option attributes are described bellow. Currently, there are no Windows specific options.
37
38#### Android
39
40Attribute | Type | Default | Description
41--------- | ---- | ------- | -----------
42`android.icon` | `string` | | Optional. The name of a drawable resource to use as the small-icon. The name should not include the extension.
43`android.iconColor` | `string` | | Optional. Sets the background color of the small icon on Android 5.0 and greater. [Supported Formats](http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String))
44`android.sound` | `boolean` | `true` | Optional. If `true` it plays the sound specified in the push data or the default system sound.
45`android.vibrate` | `boolean` | `true` | Optional. If `true` the device vibrates on receipt of notification.
46`android.clearBadge` | `boolean` | `false` | Optional. If `true` the icon badge will be cleared on init and before push messages are processed.
47`android.clearNotifications` | `boolean` | `true` | Optional. If `true` the app clears all pending notifications when it is closed.
48`android.forceShow` | `boolean` | `false` | Optional. Controls the behavior of the notification when app is in foreground. If `true` and app is in foreground, it will show a notification in the notification drawer, the same way as when the app is in background (and `on('notification')` callback will be called *only when the user clicks the notification*). When `false` and app is in foreground, the `on('notification')` callback will be called immediately.
49`android.topics` | `array` | `[]` | Optional. If the array contains one or more strings each string will be used to subscribe to a FcmPubSub topic.
50`android.messageKey` | `string` | `message` | Optional. The key to search for text of notification.
51`android.titleKey` | `string` | `'title'` | Optional. The key to search for title of notification.
52
53#### Browser
54
55Attribute | Type | Default | Description
56--------- | ---- | ------- | -----------
57`browser.pushServiceURL` | `string` | `http://push.api.phonegap.com/v1/push` | Optional. URL for the push server you want to use.
58`browser.applicationServerKey` | `string` | `` | Optional. Your GCM API key if you are using VAPID keys.
59
60#### iOS
61
62All iOS boolean options can also be specified as `string`
63
64Attribute | Type | Default | Description
65--------- | ---- | ------- | -----------
66`ios.alert` | `boolean` | `false` | Optional. If `true` the device shows an alert on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
67`ios.badge` | `boolean` | `false` | Optional. If `true` the device sets the badge number on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
68`ios.sound` | `boolean` | `false` | Optional. If `true` the device plays a sound on receipt of notification. **Note:** the value you set this option to the first time you call the init method will be how the application always acts. Once this is set programmatically in the init method it can only be changed manually by the user in Settings>Notifications>`App Name`. This is normal iOS behaviour.
69`ios.clearBadge` | `boolean` | `false` | Optional. If `true` the badge will be cleared on app startup.
70`ios.categories` | `Object` | `{}` | Optional. The data required in order to enabled Action Buttons for iOS. See [Action Buttons on iOS](https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#action-buttons-1) for more details.
71
72#### iOS GCM support
73
74The following properties are used if you want use GCM on iOS.
75
76Attribute | Type | Default | Description
77--------- | ---- | ------- | -----------
78`ios.fcmSandbox` | `boolean` | `false` | Whether to use prod or sandbox GCM setting. Defaults to false.
79options
80`ios.topics` | `array` | `[]` | Optional. If the array contains one or more strings each string will be used to subscribe to a FcmPubSub topic.
81
82##### How GCM on iOS works.
83
84First it is kind of a misnomer as GCM does not send push messages directly to devices running iOS.
85
86What happens is on the device side is that it registers with APNS, then that registration ID is sent to GCM which returns a different GCM specific ID. That is the ID you get from the push plugin `registration` event.
87
88When you send a message to GCM using that ID, what it does is look up the APNS registration ID on it's side and forward the message you sent to GCM on to APSN to deliver to your iOS device.
89
90Make sure that the certificate you build with matches your `fcmSandbox` value.
91
92- If you build your app as development and set `fcmSandbox: false` it will fail.
93- If you build your app as production and set `fcmSandbox: true` it will fail.
94- If you build your app as development and set `fcmSandbox: true` but haven't uploaded the development certs to Google it will fail.
95- If you build your app as production and set `fcmSandbox: false` but haven't uploaded the production certs to Google it will fail.
96
97> Note: The integration between GCM and APNS is a bit finicky. Personally, I feel it is much better to send pushes to Android using GCM and pushes to iOS using APNS which this plugin does support.
98
99### Example
100
101```javascript
102var push = PushNotification.init({
103 android: {
104 },
105 browser: {
106 pushServiceURL: 'http://push.api.phonegap.com/v1/push'
107 },
108 ios: {
109 alert: "true",
110 badge: true,
111 sound: 'false'
112 },
113 windows: {}
114});
115```
116
117## PushNotification.hasPermission(successHandler) - Android & iOS only
118
119Checks whether the push notification permission has been granted.
120
121### Parameters
122
123Parameter | Type | Default | Description
124--------- | ---- | ------- | -----------
125`successHandler` | `Function` | | Is called when the api successfully retrieves the details on the permission.
126
127### Callback parameters
128
129#### `successHandler`
130
131Parameter | Type | Description
132--------- | ---- | -----------
133`data.isEnabled` | `Boolean` | Whether the permission for push notifications has been granted.
134
135### Example
136
137```javascript
138PushNotification.hasPermission(function(data) {
139 if (data.isEnabled) {
140 console.log('isEnabled');
141 }
142});
143```
144
145## push.on(event, callback)
146
147### Parameters
148
149Parameter | Type | Default | Description
150--------- | ---- | ------- | -----------
151`event` | `string` | | Name of the event to listen to. See below for all the event names.
152`callback` | `Function` | | Is called when the event is triggered.
153
154## push.on('registration', callback)
155
156The event `registration` will be triggered on each successful registration with the 3rd party push service.
157
158### Callback parameters
159
160Parameter | Type | Description
161--------- | ---- | -----------
162`data.registrationId` | `string` | The registration ID provided by the 3rd party remote push service.
163`data.registrationType` | `string` | The registration type of the 3rd party remote push service. Either FCM or APNS.
164
165### Example
166
167```javascript
168push.on('registration', function(data) {
169 console.log(data.registrationId);
170 console.log(data.registrationType);
171});
172```
173
174For APNS users: the `registrationId` you will get will be a production or sandbox id according to how the app was built. ([Source](https://developer.apple.com/library/ios/technotes/tn2265/_index.html))
175
176> Note: There is a separate persistent connection to the push service for each environment. The operating system establishes a persistent connection to the sandbox environment for development builds; ad hoc and distribution builds connect to the production environment.
177
178
179
180### Common Problems
181
182#### Got JSON Exception TIMEOUT
183
184If you run this plugin on older versions of Android and you get an error:
185
186```
187E/PushPlugin(20077): execute: Got JSON Exception TIMEOUT
188```
189
190It means you are running an older version of Google Play Services. You will need to open the Google Play Store app and update your version of Google Play Services.
191
192## push.on('notification', callback)
193
194The event `notification` will be triggered each time a push notification is received by a 3rd party push service on the device.
195
196### Callback parameters
197
198Parameter | Type | Description
199--------- | ---- | -----------
200`data.message` | `string` | The text of the push message sent from the 3rd party service.
201`data.title` | `string` | The optional title of the push message sent from the 3rd party service.
202`data.count` | `string` | The number of messages to be displayed in the badge in iOS/Android or message count in the notification shade in Android. For windows, it represents the value in the badge notification which could be a number or a status glyph.
203`data.sound` | `string` | The name of the sound file to be played upon receipt of the notification.
204`data.image` | `string` | The path of the image file to be displayed in the notification.
205`data.launchArgs` | `string` | The args to be passed to the application on launch from push notification. This works when notification is received in background. (Windows Only)
206`data.additionalData` | `Object` | An optional collection of data sent by the 3rd party push service that does not fit in the above properties.
207`data.additionalData.foreground` | `boolean` | Whether the notification was received while the app was in the foreground
208`data.additionalData.coldstart` | `boolean` | Will be `true` if the application is started by clicking on the push notification, `false` if the app is already started.
209`data.additionalData.dismissed` | `boolean` | Is set to `true` if the notification was dismissed by the user
210
211### Example
212
213```javascript
214push.on('notification', function(data) {
215 console.log(data.message);
216 console.log(data.title);
217 console.log(data.count);
218 console.log(data.sound);
219 console.log(data.image);
220 console.log(data.additionalData);
221});
222```
223
224## push.on('error', callback)
225
226The event `error` will trigger when an internal error occurs and the cache is aborted.
227
228### Callback parameters
229
230Parameter | Type | Description
231--------- | ---- | -----------
232`e` | `Error` | Standard JavaScript error object that describes the error.
233
234### Example
235
236```javascript
237push.on('error', function(e) {
238 console.log(e.message);
239});
240```
241
242## push.off(event, callback)
243
244Removes a previously registered callback for an event.
245
246### Parameters
247
248Parameter | Type | Default | Description
249--------- | ---- | ------- | -----------
250`event` | `string` | | Name of the event type. The possible event names are the same as for the `push.on` function.
251`callback` | `Function` | | The same callback used to register with `push.on`.
252
253### Example
254```javascript
255var callback = function(data){ /*...*/};
256
257//Adding handler for notification event
258push.on('notification', callback);
259
260//Removing handler for notification event
261push.off('notification', callback);
262```
263
264**WARNING**: As stated in the example, you will have to store your event handler if you are planning to remove it.
265
266## push.unregister(successHandler, errorHandler, topics)
267
268The unregister method is used when the application no longer wants to receive push notifications. Beware that this cleans up all event handlers previously registered, so you will need to re-register them if you want them to function again without an application reload.
269
270If you provide a list of topics as an optional parameter then the application will unsubscribe from these topics but continue to receive other push messages.
271
272### Parameters
273
274Parameter | Type | Default | Description
275--------- | ---- | ------- | -----------
276`successHandler` | `Function` | | Is called when the api successfully unregisters.
277`errorHandler` | `Function` | | Is called when the api encounters an error while unregistering.
278`topics` | `Array` | | A list of topics to unsubscribe from.
279
280### Example
281
282```javascript
283push.unregister(function() {
284 console.log('success');
285}, function() {
286 console.log('error');
287});
288```
289
290## push.subscribe(topic, successHandler, errorHandler)
291
292The subscribe method is used when the application wants to subscribe a new topic to receive push notifications.
293
294### Parameters
295
296Parameter | Type | Default | Description
297--------- | ---- | ------- | -----------
298`topic` | `String` | | Topic to subscribe to.
299`successHandler` | `Function` | | Is called when the api successfully subscribes.
300`errorHandler` | `Function` | | Is called when the api encounters an error while subscribing.
301
302### Example
303
304```javascript
305push.subscribe('my-topic', function() {
306 console.log('success');
307}, function(e) {
308 console.log('error:');
309 console.log(e);
310});
311```
312## push.unsubscribe(topic, successHandler, errorHandler)
313
314The unsubscribe method is used when the application no longer wants to receive push notifications from a specific topic but continue to receive other push messages.
315
316### Parameters
317
318Parameter | Type | Default | Description
319--------- | ---- | ------- | -----------
320`topic` | `String` | | Topic to unsubscribe from.
321`successHandler` | `Function` | | Is called when the api successfully unsubscribe.
322`errorHandler` | `Function` | | Is called when the api encounters an error while unsubscribing.
323
324### Example
325
326```javascript
327push.unsubscribe('my-topic', function() {
328 console.log('success');
329}, function(e) {
330 console.log('error:');
331 console.log(e);
332});
333```
334
335## push.setApplicationIconBadgeNumber(successHandler, errorHandler, count) - iOS & Android only
336
337Set the badge count visible when the app is not running
338
339> Note: badges are not supported on all Android devices. See [our payload documentation](PAYLOAD.md#badges) for more details.
340
341### Parameters
342
343Parameter | Type | Default | Description
344--------- | ---- | ------- | -----------
345`successHandler` | `Function` | | Is called when the api successfully sets the icon badge number.
346`errorHandler` | `Function` | | Is called when the api encounters an error while trying to set the icon badge number.
347`count` | `number` | | Indicates what number should show up in the badge. Passing 0 will clear the badge. Each `notification` event contains a `data.count` value which can be used to set the badge to correct number.
348
349### Example
350
351```javascript
352push.setApplicationIconBadgeNumber(function() {
353 console.log('success');
354}, function() {
355 console.log('error');
356}, 2);
357```
358
359## push.getApplicationIconBadgeNumber(successHandler, errorHandler) - iOS & Android only
360
361Get the current badge count visible when the app is not running
362
363### Parameters
364
365Parameter | Type | Default | Description
366--------- | ---- | ------- | -----------
367`successHandler` | `Function` | | Is called when the api successfully retrieves the icon badge number.
368`errorHandler` | `Function` | | Is called when the api encounters an error while trying to retrieve the icon badge number.
369
370### Callback parameters
371
372#### `successHandler`
373
374Parameter | Type | Description
375--------- | ---- | -----------
376`n` | `number` | An integer which is the current badge count.
377
378### Example
379
380```javascript
381push.getApplicationIconBadgeNumber(function(n) {
382 console.log('success', n);
383}, function() {
384 console.log('error');
385});
386```
387
388## push.finish(successHandler, errorHandler, id) - iOS only
389
390Tells the OS that you are done processing a background push notification.
391
392### Parameters
393
394Parameter | Type | Default | Description
395--------- | ---- | ------- | -----------
396`successHandler` | `Function` | | Is called when the api successfully completes background push processing.
397`errorHandler` | `Function` | | Is called when the api encounters an error while processing and completing the background push.
398`id` | `String` | | Tells the OS which background process is complete.
399
400### Example
401
402```javascript
403push.finish(function() {
404 console.log('success');
405}, function() {
406 console.log('error');
407}, 'push-1');
408```
409
410## push.clearAllNotifications(successHandler, errorHandler) - iOS & Android only
411
412Tells the OS to clear all notifications from the Notification Center
413
414### Parameters
415
416Parameter | Type | Default | Description
417--------- | ---- | ------- | -----------
418`successHandler` | `Function` | | Is called when the api successfully clears the notifications.
419`errorHandler` | `Function` | | Is called when the api encounters an error when attempting to clears the notifications.
420
421### Example
422
423```javascript
424push.clearAllNotifications(function() {
425 console.log('success');
426}, function() {
427 console.log('error');
428});
429```