From 0acbe56cfb5f41d20389ef52f6225c25a163f229 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Mon, 15 Aug 2016 02:09:19 -0400 Subject: [PATCH 1/4] feat(push): plugin is now instance based no more callbacks closes #250 --- src/plugins/push.ts | 171 +++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 88 deletions(-) diff --git a/src/plugins/push.ts b/src/plugins/push.ts index dea0b2007..76b0dc487 100644 --- a/src/plugins/push.ts +++ b/src/plugins/push.ts @@ -1,5 +1,6 @@ -import { Cordova, Plugin } from './plugin'; - +import {Cordova, Plugin, CordovaInstance} from './plugin'; +import {Observable} from 'rxjs/Rx'; +declare var window: any; export type EventResponse = RegistrationEventResponse | NotificationEventResponse | Error; @@ -59,84 +60,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; - - /** - * 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; - /** - * 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 - */ - finish(successHandler: () => any, errorHandler: () => any): void; -} - export interface IOSPushOptions { /** * Maps to the project number in the Google Developer Console. Setting this @@ -265,9 +188,7 @@ export interface PushOptions { windows?: {}; } -declare var PushNotification: { - new (): PushNotification -}; +export type PushEvents = 'registeration' | 'error' | 'notification'; /** * @name Push @@ -281,6 +202,12 @@ declare var PushNotification: { * @usage * ```typescript * import { Push } from 'ionic-native'; + * + * // create new instance of Push + * let push: Push = new Push(options); + * // OR + * let push: Push = Push.init(options); + * * ``` */ @Plugin({ @@ -290,6 +217,13 @@ declare var PushNotification: { }) export class Push { + private _objectInstance: any; + + constructor(options: PushOptions) { + if (!window.PushNotification) return console.warn('Native: PushNotification is not installed, or you are not running on a device.'); + this._objectInstance = window.PushNotification.init(options); + } + /** * Initialize the plugin on the native side. * @@ -308,12 +242,9 @@ export class Push { * ``` * * @param {PushOptions} options The Push [options](https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#parameters). - * @return {PushNotification} Returns a new [PushNotification](https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushonevent-callback) object. + * @return {Push} Returns a new Push object. */ - @Cordova({ - sync: true - }) - static init(options: PushOptions): PushNotification { return; } + static init(options: PushOptions): Push { return new Push(options); } /** * Check whether the push notification permission has been granted. @@ -322,4 +253,68 @@ export class Push { @Cordova() static hasPermission(): Promise<{ isEnabled: boolean }> { return; } + /** + * Adds an event listener + * @param event + */ + @CordovaInstance({ + observable: true, + clearFunction: 'off', + clearWithArgs: true + }) + on(event: string): Observable { + 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 { + 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 { + 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 { + 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 { + return; + } + + /** + * Tells the OS to clear all notifications from the Notification Center + */ + @CordovaInstance() + clearAllNotifications(): Promise { + return; + } + } From 3aaf92090a21d903285265f0219da446bf412f30 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Mon, 19 Sep 2016 18:03:58 -0400 Subject: [PATCH 2/4] fix(push): fix typo in PushEvents enum --- src/plugins/push.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/push.ts b/src/plugins/push.ts index 76b0dc487..65bfea9a6 100644 --- a/src/plugins/push.ts +++ b/src/plugins/push.ts @@ -188,7 +188,7 @@ export interface PushOptions { windows?: {}; } -export type PushEvents = 'registeration' | 'error' | 'notification'; +export type PushEvents = 'registration' | 'error' | 'notification'; /** * @name Push From 22563233a3f23c6b011e9b6cf918892576906859 Mon Sep 17 00:00:00 2001 From: Ibby Hadeed Date: Thu, 1 Dec 2016 18:58:13 -0500 Subject: [PATCH 3/4] fix constructor --- src/plugins/push.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/plugins/push.ts b/src/plugins/push.ts index 65bfea9a6..c300345ab 100644 --- a/src/plugins/push.ts +++ b/src/plugins/push.ts @@ -220,7 +220,10 @@ export class Push { private _objectInstance: any; constructor(options: PushOptions) { - if (!window.PushNotification) return console.warn('Native: PushNotification is not installed, or you are not running on a device.'); + if (!window.PushNotification) { + console.warn('Native: PushNotification is not installed, or you are not running on a device.'); + return; + } this._objectInstance = window.PushNotification.init(options); } From abe5d58f9d422effe815977dbaf3b50f2a3c0d97 Mon Sep 17 00:00:00 2001 From: Ibby Date: Thu, 15 Dec 2016 08:16:22 -0500 Subject: [PATCH 4/4] feat(push): add subscribe/unsubscribe fixes #861 --- src/plugins/push.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/plugins/push.ts b/src/plugins/push.ts index 6684c1d88..201b01826 100644 --- a/src/plugins/push.ts +++ b/src/plugins/push.ts @@ -331,4 +331,20 @@ export class Push { 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} + */ + @CordovaInstance() + subscribe(topic: string): Promise { 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} + */ + @CordovaInstance() + unsubscribe(topic: string): Promise { return; } + }