2017-12-28 07:28:44 -05:00
|
|
|
import { Cordova, IonicNativePlugin, Plugin } from '@ionic-native/core';
|
2017-05-09 17:29:29 -04:00
|
|
|
import { Injectable } from '@angular/core';
|
2018-04-05 21:44:19 +02:00
|
|
|
import { Observable } from 'rxjs';
|
2017-05-09 17:29:29 -04:00
|
|
|
|
|
|
|
export interface NotificationData {
|
|
|
|
/**
|
|
|
|
* Determines whether the notification was pressed or not
|
|
|
|
*/
|
|
|
|
|
|
|
|
wasTapped: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification data hash item
|
|
|
|
*/
|
|
|
|
|
|
|
|
[name: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name FCM
|
|
|
|
* @description
|
|
|
|
* Provides basic functionality for Firebase Cloud Messaging
|
|
|
|
*
|
|
|
|
* @usage
|
|
|
|
* ```typescript
|
2018-10-10 16:13:45 -05:00
|
|
|
* import { FCM } from '@ionic-native/fcm/ngx';
|
2017-05-09 17:29:29 -04:00
|
|
|
*
|
|
|
|
* constructor(private fcm: FCM) {}
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* ...
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2018-03-02 02:00:17 +05:30
|
|
|
* this.fcm.subscribeToTopic('marketing');
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2018-03-17 01:22:06 +01:00
|
|
|
* this.fcm.getToken().then(token => {
|
2017-05-09 17:29:29 -04:00
|
|
|
* backend.registerToken(token);
|
2018-03-17 01:22:06 +01:00
|
|
|
* });
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2018-03-17 01:22:06 +01:00
|
|
|
* this.fcm.onNotification().subscribe(data => {
|
2017-05-19 16:06:04 +05:30
|
|
|
* if(data.wasTapped){
|
2017-05-09 17:29:29 -04:00
|
|
|
* console.log("Received in background");
|
|
|
|
* } else {
|
|
|
|
* console.log("Received in foreground");
|
|
|
|
* };
|
2018-03-17 01:22:06 +01:00
|
|
|
* });
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2018-03-17 01:22:06 +01:00
|
|
|
* this.fcm.onTokenRefresh().subscribe(token => {
|
2017-05-09 17:29:29 -04:00
|
|
|
* backend.registerToken(token);
|
2018-03-17 01:22:06 +01:00
|
|
|
* });
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2018-03-02 02:00:17 +05:30
|
|
|
* this.fcm.unsubscribeFromTopic('marketing');
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* ```
|
|
|
|
* @interfaces
|
|
|
|
* NotificationData
|
|
|
|
*/
|
|
|
|
@Plugin({
|
|
|
|
pluginName: 'FCM',
|
2018-07-18 15:50:16 -04:00
|
|
|
plugin: 'cordova-plugin-fcm-with-dependecy-updated',
|
2017-05-09 17:29:29 -04:00
|
|
|
pluginRef: 'FCMPlugin',
|
2018-07-18 15:50:16 -04:00
|
|
|
repo: 'https://github.com/andrehtissot/cordova-plugin-fcm-with-dependecy-updated',
|
2017-06-02 21:34:18 +02:00
|
|
|
platforms: ['Android', 'iOS']
|
2017-05-09 17:29:29 -04:00
|
|
|
})
|
2019-02-20 17:37:39 +01:00
|
|
|
@Injectable()
|
2017-05-09 17:29:29 -04:00
|
|
|
export class FCM extends IonicNativePlugin {
|
|
|
|
/**
|
2018-04-08 21:26:14 +02:00
|
|
|
* Gets device's current registration id
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @returns {Promise<string>} Returns a Promise that resolves with the registration id token
|
|
|
|
*/
|
|
|
|
@Cordova()
|
2017-12-28 07:28:44 -05:00
|
|
|
getToken(): Promise<string> {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-09 17:29:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event firing on the token refresh
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @returns {Observable<string>} Returns an Observable that notifies with the change of device's registration id
|
|
|
|
*/
|
|
|
|
@Cordova({
|
|
|
|
observable: true
|
|
|
|
})
|
2017-12-28 07:28:44 -05:00
|
|
|
onTokenRefresh(): Observable<string> {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-09 17:29:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribes you to a [topic](https://firebase.google.com/docs/notifications/android/console-topics)
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @param {string} topic Topic to be subscribed to
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @returns {Promise<any>} Returns a promise resolving in result of subscribing to a topic
|
|
|
|
*/
|
|
|
|
@Cordova()
|
2017-12-28 07:28:44 -05:00
|
|
|
subscribeToTopic(topic: string): Promise<any> {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-09 17:29:29 -04:00
|
|
|
|
|
|
|
/**
|
2018-04-08 21:26:14 +02:00
|
|
|
* Unsubscribes you from a [topic](https://firebase.google.com/docs/notifications/android/console-topics)
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @param {string} topic Topic to be unsubscribed from
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @returns {Promise<any>} Returns a promise resolving in result of unsubscribing from a topic
|
|
|
|
*/
|
|
|
|
@Cordova()
|
2017-12-28 07:28:44 -05:00
|
|
|
unsubscribeFromTopic(topic: string): Promise<any> {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-09 17:29:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch for incoming notifications
|
2017-06-02 21:34:18 +02:00
|
|
|
*
|
2017-05-09 17:29:29 -04:00
|
|
|
* @returns {Observable<any>} returns an object with data from the notification
|
|
|
|
*/
|
|
|
|
@Cordova({
|
|
|
|
observable: true,
|
|
|
|
successIndex: 0,
|
|
|
|
errorIndex: 2
|
|
|
|
})
|
2017-12-28 07:28:44 -05:00
|
|
|
onNotification(): Observable<NotificationData> {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-09 17:29:29 -04:00
|
|
|
}
|