push instance based push plugin

This commit is contained in:
Ibby
2017-03-11 14:40:02 -05:00
+128 -141
View File
@@ -1,6 +1,8 @@
import { Injectable } from '@angular/core';
import { Cordova, Plugin } from '@ionic-native/core';
import { Cordova, Plugin, CordovaInstance, checkAvailability } from '@ionic-native/core';
import { Observable } from 'rxjs/Observable';
declare var window: any;
export type EventResponse = RegistrationEventResponse | NotificationEventResponse | Error;
@@ -41,8 +43,6 @@ export interface NotificationEventResponse {
}
/**
* TODO: document all possible properties (not just Android)
*
* Loosened up with a dictionary notation, but all non-defined properties need to use (map['prop']) notation
*
* Ideally the developer would overload (merged declaration) this or create a new interface that would extend this one
@@ -61,117 +61,6 @@ export interface NotificationEventAdditionalData {
notId?: string;
}
export interface PushNotification {
/**
* The event registration will be triggered on each successful registration with the 3rd party push service.
* @param event
* @param callback
*/
on(event: 'registration', callback: (response: RegistrationEventResponse) => any): void;
/**
* The event notification will be triggered each time a push notification is received by a 3rd party push service on the device.
* @param event
* @param callback
*/
on(event: 'notification', callback: (response: NotificationEventResponse) => any): void;
/**
* The event error will trigger when an internal error occurs and the cache is aborted.
* @param event
* @param callback
*/
on(event: 'error', callback: (response: Error) => any): void;
/**
*
* @param event Name of the event to listen to. See below(above) for all the event names.
* @param callback is called when the event is triggered.
* @param event
* @param callback
*/
on(event: string, callback: (response: EventResponse) => any): void;
off(event: 'registration', callback: (response: RegistrationEventResponse) => any): void;
off(event: 'notification', callback: (response: NotificationEventResponse) => any): void;
off(event: 'error', callback: (response: Error) => any): void;
/**
* As stated in the example, you will have to store your event handler if you are planning to remove it.
* @param event Name of the event type. The possible event names are the same as for the push.on function.
* @param callback handle to the function to get removed.
* @param event
* @param callback
*/
off(event: string, callback: (response: EventResponse) => any): void;
/**
* The 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.
* @param successHandler
* @param errorHandler
*/
unregister(successHandler: () => any, errorHandler?: () => any): void;
/**
* The subscribe method is used when the application wants to subscribe a new topic to receive push notifications.
* @param {string} topic: Topic to subscribe to.
* @param successHandler
* @param errorHandler
*/
subscribe(topic: string, successHandler: () => any, errorHandler?: () => any): void;
/**
* The 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.
* @param {string} topic: Topic to subscribe to.
* @param successHandler
* @param errorHandler
*/
unsubscribe(topic: string, successHandler: () => any, errorHandler?: () => any): void;
/**
* iOS & android only
*
* Set the badge count visible when the app is not running
*
* The count is an integer indicating 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.
* @param successHandler
* @param errorHandler
* @param count
*/
setApplicationIconBadgeNumber(successHandler: () => any, errorHandler: () => any, count?: number): void;
/**
* iOS only
*
* Get the current badge count visible when the app is not running
* successHandler gets called with an integer which is the current badge count
* @param successHandler
* @param errorHandler
*/
getApplicationIconBadgeNumber(successHandler: (count: number) => any, errorHandler: () => any): void;
/**
* iOS only
*
* Tells the OS that you are done processing a background push notification.
* successHandler gets called when background push processing is successfully completed.
* @param successHandler
* @param errorHandler
* @param id
*/
finish(successHandler: () => any, errorHandler: () => any, id?: string): void;
/**
* iOS & android only
*
* Tells the OS to clear all notifications from the Notification Center.
* successHandler gets called when the api successfully clears the notifications
* @param successHandler
* @param errorHandler
*/
clearAllNotifications(successHandler: () => any, errorHandler: () => any): void;
}
export interface IOSPushOptions {
/**
* Maps to the project number in the Google Developer Console. Setting this
@@ -302,12 +191,10 @@ export interface AndroidPushOptions {
export interface PushOptions {
ios?: IOSPushOptions;
android?: AndroidPushOptions;
windows?: {};
windows?: any;
}
declare var PushNotification: {
new (): PushNotification
};
export type PushEvent = 'registration' | 'error' | 'notification';
/**
* @name Push
@@ -320,8 +207,40 @@ declare var PushNotification: {
*
* @usage
* ```typescript
* import { Push } from '@ionic-native/push';
* import { Push, PushObject, PushOptions } from '@ionic-native/push';
*
* constructor(private push: Push) { }
*
* ...
*
* const options: PushOptions = {
* android: {
* senderID: '12345679'
* },
* ios: {
* alert: 'true',
* badge: true,
* sound: 'false'
* },
* windows: {}
* };
*
* const pushObject: PushObject = this.push.init(options);
*
* pushObject.on('notification').subscribe(notification => console.log('Received a notification', notification));
*
* pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
*
*
* ```
*
* @interfaces
* RegistrationEventResponse
* NotificationEventResponse
* NotificationEventAdditionalData
* IOSPushOptions
* AndroidPushOptions
* PushOptions
*/
@Plugin({
pluginName: 'Push',
@@ -335,29 +254,28 @@ declare var PushNotification: {
export class Push {
/**
* Initialize the plugin on the native side.
*
* ```
* var push = Push.init({
* android: {
* senderID: '12345679'
* },
* ios: {
* alert: 'true',
* badge: true,
* sound: 'false'
* },
* windows: {}
* });
* ```
*
* @param {PushOptions} options The Push [options](https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#parameters).
* @returns {PushNotification} Returns a new [PushNotification](https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushonevent-callback) object.
* Init push notifications
* @param options {PushOptions}
* @return {PushObject}
*/
@Cordova({
sync: true
})
init(options: PushOptions): PushNotification { return; }
init(options: PushOptions): PushObject {
return new PushObject(options);
}
}
/**
* @private
*/
export class PushObject {
private _objectInstance: any;
constructor(options: PushOptions) {
if (checkAvailability('PushNotification', 'init', 'PushNotification') === true) {
this._objectInstance = window.PushNotification.init(options);
}
}
/**
* Check whether the push notification permission has been granted.
@@ -366,4 +284,73 @@ export class Push {
@Cordova()
hasPermission(): Promise<{ isEnabled: boolean }> { return; }
/**
* Adds an event listener
* @param event {string}
* @return {Observable<EventResponse>}
*/
@CordovaInstance({
observable: true,
clearFunction: 'off',
clearWithArgs: true
})
on(event: PushEvent): Observable<EventResponse> { return; }
/**
* The 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.
*/
@CordovaInstance()
unregister(): Promise<any> { return; }
/**
* Set the badge count visible when the app is not running
*
* The count is an integer indicating 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.
* @param count
*/
@CordovaInstance({
callbackOrder: 'reverse'
})
setApplicationIconBadgeNumber(count?: number): Promise<any> { return; };
/**
* Get the current badge count visible when the app is not running
* successHandler gets called with an integer which is the current badge count
*/
@CordovaInstance()
getApplicationIconBadgeNumber(): Promise<number> { return; }
/**
* iOS only
* Tells the OS that you are done processing a background push notification.
* successHandler gets called when background push processing is successfully completed.
*/
@CordovaInstance()
finish(): Promise<any> { return; }
/**
* Tells the OS to clear all notifications from the Notification Center
*/
@CordovaInstance()
clearAllNotifications(): Promise<any> { return; }
/**
* The subscribe method is used when the application wants to subscribe a new topic to receive push notifications.
* @param topic {string} Topic to subscribe to.
* @return {Promise<any>}
*/
@CordovaInstance()
subscribe(topic: string): Promise<any> { return; }
/**
* The 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.
* @param topic {string} Topic to unsubscribe from.
* @return {Promise<any>}
*/
@CordovaInstance()
unsubscribe(topic: string): Promise<any> { return; }
}