import { Injectable } from '@angular/core';
import { Cordova, Plugin, IonicNativePlugin } from '@ionic-native/core';
import { Observable } from 'rxjs/Observable';
export interface OSNotification {
/**
* Was app in focus.
*/
isAppInFocus: boolean;
/**
* Was notification shown to the user. Will be false for silent notifications.
*/
shown: boolean;
/**
* **ANDROID** - Android Notification assigned to the notification. Can be used to cancel or replace the notification.
*/
androidNotificationId?: number;
/**
* Payload received from OneSignal.
*/
payload: OSNotificationPayload;
/**
* How the notification was displayed to the user. Can be set to `Notification`, `InAppAlert`, or `None` if it was not displayed.
*/
displayType: OSDisplayType;
/**
* **ANDROID** - Notification is a summary notification for a group this will contain all notification payloads it was created from.
*/
groupedNotifications?: OSNotificationPayload[];
app_id?: string;
contents: any;
headings?: any;
isIos?: boolean;
isAndroid?: boolean;
isWP?: boolean;
isWP_WNS?: boolean;
isAdm?: boolean;
isChrome?: boolean;
isChromeWeb?: boolean;
isSafari?: boolean;
isAnyWeb?: boolean;
included_segments?: string[];
excluded_segments?: string[];
include_player_ids?: string[];
include_ios_tokens?: string[];
include_android_reg_ids?: string[];
include_wp_uris?: string[];
include_wp_wns_uris?: string[];
include_amazon_reg_ids?: string[];
include_chrome_reg_ids?: string[];
include_chrome_web_reg_ids?: string[];
app_ids?: string[];
tags?: any[];
ios_badgeType?: string;
ios_badgeCount?: number;
ios_sound?: string;
android_sound?: string;
adm_sound?: string;
wp_sound?: string;
wp_wns_sound?: string;
data?: any;
buttons?: any;
small_icon?: string;
large_icon?: string;
big_picture?: string;
adm_small_icon?: string;
adm_large_icon?: string;
adm_big_picture?: string;
chrome_icon?: string;
chrome_big_picture?: string;
chrome_web_icon?: string;
firefox_icon?: string;
url?: string;
send_after?: string;
delayed_option?: string;
delivery_time_of_day?: string;
android_led_color?: string;
android_accent_color?: string;
android_visibility?: number;
content_available?: boolean;
amazon_background_data?: boolean;
template_id?: string;
android_group?: string;
android_group_message?: any;
adm_group?: string;
adm_group_message?: any;
ttl?: number;
priority?: number;
ios_category?: string;
}
/**
* **ANDROID** - Privacy setting for how the notification should be shown on the lockscreen of Android 5+ devices.
*/
export enum OSLockScreenVisibility {
/**
* Fully visible (default)
*/
Public = 1,
/**
* Contents are hidden
*/
Private = 0,
/**
* Not shown
*/
Secret = -1
}
/**
* How the notification was displayed to the user. Part of OSNotification. See inFocusDisplaying for more information on how this is used.
*/
export enum OSDisplayType {
/**
* notification is silent, or inFocusDisplaying is disabled.
*/
None = 0,
/**
* (**DEFAULT**) - native alert dialog display.
*/
InAppAlert = 1,
/**
* native notification display.
*/
Notification = 2
}
/**
* Contents and settings of the notification the user received.
*/
export interface OSNotificationPayload {
/**
* OneSignal notification UUID.
*/
notificationID: string;
/**
* Title of the notification.
*/
title: string;
/**
* Body of the notification.
*/
body: string;
/**
* Custom additional data that was sent with the notification. Set on the dashboard under Options > Additional Data
* or with the 'data' field on the REST API.
*/
additionalData?: any;
/**
* **ANDROID** - Small icon resource name set on the notification.
*/
smallIcon?: string;
/**
* **ANDROID** - Large icon set on the notification.
*/
largeIcon?: string;
/**
* **ANDROID** - Big picture image set on the notification.
*/
bigPicture?: string;
/**
* **ANDROID** - Accent color shown around small notification icon on Android 5+ devices. ARGB format.
*/
smallIconAccentColor?: string;
/**
* URL to open when opening the notification.
*/
launchURL?: string;
/**
* Sound resource to play when the notification is shown.
*/
sound: string;
/**
* **ANDROID** - Devices that have a notification LED will blink in this color. ARGB format.
*/
ledColor?: string;
lockScreenVisibility?: OSLockScreenVisibility;
/**
* **ANDROID** - Notifications with this same key will be grouped together as a single summary notification.
*/
groupKey?: string;
/**
* **ANDROID** - Summary text displayed in the summary notification.
*/
groupMessage?: string;
/**
* List of action buttons on the notification.
*/
actionButtons: OSActionButton[];
/**
* **ANDROID** - The Google project number the notification was sent under.
*/
fromProjectNumber?: string;
/**
* **ANDROID** - If a background image was set this object will be available.
*/
backgroundImageLayout?: OSBackgroundImageLayout;
priority?: number;
/**
* List of action buttons on the notification.
*/
rawPayload: string;
}
/**
* List of action buttons on the notification.
*/
export interface OSActionButton {
/**
* Id assigned to the button.
*/
id: string;
/**
* Text show on the button to the user.
*/
text: string;
/**
* **ANDROID** - Icon shown on the button.
*/
icon: string;
}
/**
* OSPermissionState
*/
export interface OSPermissionState {
/**
* User was prompted.
*/
hasPrompted: boolean;
/**
* Permissions Status
*/
status: any;
}
/**
* OSSubscriptionState
*/
export interface OSSubscriptionState {
subscribed: boolean;
userSubscriptionSetting: any;
userId: any;
pushToken: any;
}
/**
* Subscription and permissions status
*/
export interface OSPermissionSubscriptionState {
/**
* Id assigned to the button.
*/
permissionStatus: OSPermissionState;
/**
* Text show on the button to the user.
*/
subscriptionStatus: OSSubscriptionState;
}
/**
* **ANDROID** - If a background image was set, this object will be available.
*/
export interface OSBackgroundImageLayout {
/**
* Image URL or name used as the background image.
*/
image: string;
/**
* Text color of the title on the notification. ARGB Format.
*/
titleTextColor: string;
/**
* Text color of the body on the notification. ARGB Format.
*/
bodyTextColor: string;
}
/**
* The information returned from a notification the user received.
*/
export interface OSNotificationOpenedResult {
action: {
/**
* Was the notification opened normally (`Opened`) or was a button pressed on the notification (`ActionTaken`).
*/
type: OSActionType;
/**
* If `type` == `ActionTaken` then this will contain the id of the button pressed.
*/
actionID?: string;
};
notification: OSNotification;
}
export enum OSActionType {
Opened = 0,
ActionTake = 1
}
/**
* @name OneSignal
* @description
* The OneSignal plugin is an client implementation for using the [OneSignal](https://onesignal.com/) Service.
* OneSignal is a simple implementation for delivering push notifications.
*
* Please view the official [OneSignal Ionic SDK Installation](https://documentation.onesignal.com/docs/ionic-sdk-setup) guide
* for more information.
*
* #### Icons
* If you want to use generated icons with command `ionic cordova resources`:
*
* 1. Add a file to your `hooks` directory called `copy_android_notification_icons.js`
*
* 2. Configure the hook in your config.xml
* ```
*
*
*
* ```
*
* 3. Put the following code in it:
*
* ```
* #!/usr/bin/env node
* var fs = require('fs');
* var path = require('path');
* var filestocopy = [{
* "resources/android/icon/drawable-hdpi-icon.png":
* "platforms/android/res/drawable-hdpi/ic_stat_onesignal_default.png"
* }, {
* "resources/android/icon/drawable-mdpi-icon.png":
* "platforms/android/res/drawable-mdpi/ic_stat_onesignal_default.png"
* }, {
* "resources/android/icon/drawable-xhdpi-icon.png":
* "platforms/android/res/drawable-xhdpi/ic_stat_onesignal_default.png"
* }, {
* "resources/android/icon/drawable-xxhdpi-icon.png":
* "platforms/android/res/drawable-xxhdpi/ic_stat_onesignal_default.png"
* }, {
* "resources/android/icon/drawable-xxxhdpi-icon.png":
* "platforms/android/res/drawable-xxxhdpi/ic_stat_onesignal_default.png"
* } ];
* module.exports = function(context) {
* // no need to configure below
* var rootdir = context.opts.projectRoot;
* filestocopy.forEach(function(obj) {
* Object.keys(obj).forEach(function(key) {
* var val = obj[key];
* var srcfile = path.join(rootdir, key);
* var destfile = path.join(rootdir, val);
* console.log("copying "+srcfile+" to "+destfile);
* var destdir = path.dirname(destfile);
* if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {
* fs.createReadStream(srcfile).pipe(
* fs.createWriteStream(destfile));
* }
* });
* });
* };
* ```
*
* 3. From the root of your project make the file executable:
* `$ chmod +x hooks/after_prepare/030_copy_android_notification_icons.js`
*
*
* @usage
* ```typescript
* import { OneSignal } from '@ionic-native/onesignal';
*
* constructor(private oneSignal: OneSignal) { }
*
* ...
*
* this.oneSignal.startInit('b2f7f966-d8cc-11e4-bed1-df8f05be55ba', '703322744261');
*
* this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
*
* this.oneSignal.handleNotificationReceived().subscribe(() => {
* // do something when notification is received
* });
*
* this.oneSignal.handleNotificationOpened().subscribe(() => {
* // do something when a notification is opened
* });
*
* this.oneSignal.endInit();
* ```
* @interfaces
* OSNotification
* OSLockScreenVisibility
* OSDisplayType
* OSNotificationPayload
* OSActionButton
* OSBackgroundImageLayout
* OSNotificationOpenedResult
* OSActionType
*/
@Plugin({
pluginName: 'OneSignal',
plugin: 'onesignal-cordova-plugin',
pluginRef: 'plugins.OneSignal',
repo: 'https://github.com/OneSignal/OneSignal-Cordova-SDK',
platforms: ['Amazon Fire OS', 'Android', 'iOS', 'Windows']
})
@Injectable()
export class OneSignal extends IonicNativePlugin {
/**
* constants to use in inFocusDisplaying()
*/
OSInFocusDisplayOption = {
None: 0,
InAppAlert: 1,
Notification: 2
};
/**
* Start the initialization process. Once you are done configuring OneSignal, call the `endInit` function.
*
* @param {string} appId Your OneSignal app id
* @param {string} googleProjectNumber **ANDROID** - your Google project number; only required for Android GCM/FCM pushes.
* @returns {any}
*/
@Cordova({ sync: true })
startInit(appId: string, googleProjectNumber?: string): any {
return;
}
/**
* Callback to run when a notification is received, whether it was displayed or not.
*
* @return {Observable}
*/
@Cordova({
observable: true
})
handleNotificationReceived(): Observable {
return;
}
/**
* Callback to run when a notification is tapped on from the notification shade (**ANDROID**) or notification
* center (**iOS**), or when closing an Alert notification shown in the app (if InAppAlert is enabled in
* inFocusDisplaying).
*
* @return {Observable}
*/
@Cordova({
observable: true
})
handleNotificationOpened(): Observable {
return;
}
/**
* **iOS** - Settings for iOS apps
*
* @param settings
* kOSSettingsKeyAutoPrompt: boolean = true
* Auto prompt user for notification permissions.
*
* kOSSettingsKeyInAppLaunchURL: boolean = false
* Launch notifications with a launch URL as an in app webview.
* @returns {any}
*/
@Cordova({
sync: true,
platforms: ['iOS']
})
iOSSettings(settings: {
kOSSettingsKeyAutoPrompt: boolean;
kOSSettingsKeyInAppLaunchURL: boolean;
}): any {
return;
}
/**
* Must be called after `startInit` to complete initialization of OneSignal.
*
* @returns {any}
*/
@Cordova({ sync: true })
endInit(): any {
return;
}
/**
* Prompt the user for notification permissions. Callback fires as soon as the user accepts or declines notifications.
* @returns {Promise}
*/
@Cordova({
platforms: ['iOS']
})
promptForPushNotificationsWithUserResponse(): Promise {
return;
}
/**
* Retrieve a list of tags that have been set on the user from the OneSignal server.
*
* **Quirk**: You must wait for `getTags` to resolve before calling it again, as the plugin will only process the last method call and discard any previous ones.
*
* @returns {Promise} Returns a Promise that resolves when tags are recieved.
*/
@Cordova()
getTags(): Promise {
return;
}
/**
* Lets you retrieve the OneSignal user id and device token.
* Your handler is called after the device is successfully registered with OneSignal.
*
* @returns {Promise