New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@nativescript/local-notifications

Package Overview
Dependencies
Maintainers
18
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nativescript/local-notifications - npm Package Compare versions

Comparing version 6.0.0 to 6.1.0

LICENSE

5

index.android.d.ts

@@ -15,4 +15,9 @@ import { LocalNotificationsApi, LocalNotificationsCommon, ReceivedNotification, ScheduleOptions } from './common';

schedule(scheduleOptions: ScheduleOptions[]): Promise<Array<number>>;
private static ensurePreconditions;
private static canSend;
private static areEnabled;
private static hasPermission;
private static requestPermission;
private static isAuthorized;
}
export declare const LocalNotifications: LocalNotificationsImpl;

134

index.android.js
import { Application, Device, Utils } from '@nativescript/core';
import { check, request } from '@nativescript-community/perms';
import { LocalNotificationsCommon } from './common';

@@ -67,24 +68,20 @@ const NotificationManagerCompatPackageName = useAndroidX() ? global.androidx.core.app : android.support.v4.app;

}
hasPermission() {
return new Promise((resolve, reject) => {
try {
resolve(LocalNotificationsImpl.hasPermission());
}
catch (ex) {
console.log('Error in LocalNotifications.hasPermission: ' + ex);
reject(ex);
}
});
async hasPermission() {
try {
return await LocalNotificationsImpl.canSend();
}
catch (ex) {
console.log('Error in LocalNotifications.hasPermission: ' + ex);
throw ex;
}
}
requestPermission() {
return new Promise((resolve, reject) => {
try {
// AFAIK can't do it on this platform.. when 'false' is returned, the app could prompt the user to manually enable them in the Device Settings
resolve(LocalNotificationsImpl.hasPermission());
}
catch (ex) {
console.log('Error in LocalNotifications.requestPermission: ' + ex);
reject(ex);
}
});
async requestPermission() {
try {
await LocalNotificationsImpl.ensurePreconditions();
return true;
}
catch (ex) {
console.log('Error in LocalNotifications.requestPermission: ' + ex);
return false;
}
}

@@ -176,42 +173,71 @@ addOnMessageReceivedCallback(onReceived) {

}
schedule(scheduleOptions) {
return new Promise((resolve, reject) => {
try {
if (!LocalNotificationsImpl.hasPermission()) {
reject('Permission not granted');
return;
async schedule(scheduleOptions) {
try {
await LocalNotificationsImpl.ensurePreconditions();
const context = Utils.android.getApplicationContext();
const resources = context.getResources();
const scheduledIds = [];
// TODO: All these changes in the options (other than setting the ID) should rather be done in Java so that
// the persisted options are exactly like the original ones.
for (let n in scheduleOptions) {
const options = LocalNotificationsImpl.merge(scheduleOptions[n], LocalNotificationsImpl.defaults);
options.icon = LocalNotificationsImpl.getIcon(context, resources, (LocalNotificationsImpl.IS_GTE_LOLLIPOP && options.silhouetteIcon) || options.icon);
options.atTime = options.at ? options.at.getTime() : 0;
// Used when restoring the notification after a reboot:
options.repeatInterval = LocalNotificationsImpl.getInterval(options.interval);
if (options.color) {
options.color = options.color.android;
}
const context = Utils.android.getApplicationContext();
const resources = context.getResources();
const scheduledIds = [];
// TODO: All these changes in the options (other than setting the ID) should rather be done in Java so that
// the persisted options are exactly like the original ones.
for (let n in scheduleOptions) {
const options = LocalNotificationsImpl.merge(scheduleOptions[n], LocalNotificationsImpl.defaults);
options.icon = LocalNotificationsImpl.getIcon(context, resources, (LocalNotificationsImpl.IS_GTE_LOLLIPOP && options.silhouetteIcon) || options.icon);
options.atTime = options.at ? options.at.getTime() : 0;
// Used when restoring the notification after a reboot:
options.repeatInterval = LocalNotificationsImpl.getInterval(options.interval);
if (options.color) {
options.color = options.color.android;
}
if (options.notificationLed && options.notificationLed !== true) {
options.notificationLed = options.notificationLed.android;
}
LocalNotificationsImpl.ensureID(options);
com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(new org.json.JSONObject(JSON.stringify(options)), context);
scheduledIds.push(options.id);
if (options.notificationLed && options.notificationLed !== true) {
options.notificationLed = options.notificationLed.android;
}
resolve(scheduledIds);
LocalNotificationsImpl.ensureID(options);
com.telerik.localnotifications.LocalNotificationsPlugin.scheduleNotification(new org.json.JSONObject(JSON.stringify(options)), context);
scheduledIds.push(options.id);
}
catch (ex) {
console.log('Error in LocalNotifications.schedule: ' + ex);
reject(ex);
}
});
return scheduledIds;
}
catch (ex) {
console.log('Error in LocalNotifications.schedule: ' + ex);
throw ex;
}
}
static hasPermission() {
static async ensurePreconditions() {
const hasPermission = await LocalNotificationsImpl.hasPermission();
if (!hasPermission) {
const granted = await LocalNotificationsImpl.requestPermission();
if (!granted)
throw new Error('Permission not granted');
}
// AFAIK can't do it on this platform. when 'false' is returned, the app could prompt the user to manually enable them in the Device Settings
const enabled = LocalNotificationsImpl.areEnabled();
if (!enabled)
throw new Error('Notifications were manually disabled');
}
static async canSend() {
const hasPermission = await LocalNotificationsImpl.hasPermission();
const areEnabled = LocalNotificationsImpl.areEnabled();
return hasPermission && areEnabled;
}
static areEnabled() {
const context = Utils.android.getApplicationContext();
return !context || NotificationManagerCompatPackageName.NotificationManagerCompat.from(context).areNotificationsEnabled();
}
static async hasPermission() {
const result = await check('notification');
return LocalNotificationsImpl.isAuthorized(result);
}
static async requestPermission() {
try {
const result = await request('notification');
return LocalNotificationsImpl.isAuthorized(result);
}
catch (ex) {
return false;
}
}
static isAuthorized(result) {
const [status, _] = result;
return status === 'authorized';
}
}

@@ -218,0 +244,0 @@ LocalNotificationsImpl.IS_GTE_LOLLIPOP = android.os.Build.VERSION.SDK_INT >= 21;

@@ -33,3 +33,3 @@ import { File, ImageSource, knownFolders, path as fsPath } from '@nativescript/core';

const settings = UIApplication.sharedApplication.currentUserNotificationSettings;
const types = 4 /* Alert */ | 1 /* Badge */ | 2 /* Sound */;
const types = 4 /* UIUserNotificationType.Alert */ | 1 /* UIUserNotificationType.Badge */ | 2 /* UIUserNotificationType.Sound */;
return (settings.types & types) > 0;

@@ -46,18 +46,18 @@ }

if (interval === 'minute') {
return 128 /* CalendarUnitSecond */;
return 128 /* NSCalendarUnit.CalendarUnitSecond */;
}
else if (interval === 'hour') {
return 64 /* CalendarUnitMinute */ | 128 /* CalendarUnitSecond */;
return 64 /* NSCalendarUnit.CalendarUnitMinute */ | 128 /* NSCalendarUnit.CalendarUnitSecond */;
}
else if (interval === 'day') {
return 32 /* CalendarUnitHour */ | 64 /* CalendarUnitMinute */ | 128 /* CalendarUnitSecond */;
return 32 /* NSCalendarUnit.CalendarUnitHour */ | 64 /* NSCalendarUnit.CalendarUnitMinute */ | 128 /* NSCalendarUnit.CalendarUnitSecond */;
}
else if (interval === 'week') {
return 512 /* CalendarUnitWeekday */ | 32 /* CalendarUnitHour */ | 64 /* CalendarUnitMinute */ | 128 /* CalendarUnitSecond */;
return 512 /* NSCalendarUnit.CalendarUnitWeekday */ | 32 /* NSCalendarUnit.CalendarUnitHour */ | 64 /* NSCalendarUnit.CalendarUnitMinute */ | 128 /* NSCalendarUnit.CalendarUnitSecond */;
}
else if (interval === 'month') {
return 16 /* CalendarUnitDay */ | 32 /* CalendarUnitHour */ | 64 /* CalendarUnitMinute */ | 128 /* CalendarUnitSecond */;
return 16 /* NSCalendarUnit.CalendarUnitDay */ | 32 /* NSCalendarUnit.CalendarUnitHour */ | 64 /* NSCalendarUnit.CalendarUnitMinute */ | 128 /* NSCalendarUnit.CalendarUnitSecond */;
}
else if (interval === 'year') {
return 8 /* CalendarUnitMonth */ | 16 /* CalendarUnitDay */ | 32 /* CalendarUnitHour */ | 64 /* CalendarUnitMinute */ | 128 /* CalendarUnitSecond */;
return 8 /* NSCalendarUnit.CalendarUnitMonth */ | 16 /* NSCalendarUnit.CalendarUnitDay */ | 32 /* NSCalendarUnit.CalendarUnitHour */ | 64 /* NSCalendarUnit.CalendarUnitMinute */ | 128 /* NSCalendarUnit.CalendarUnitSecond */;
}

@@ -68,3 +68,3 @@ else if (typeof interval === 'number') {

else {
return 4 /* CalendarUnitYear */ | 8 /* CalendarUnitMonth */ | 16 /* CalendarUnitDay */ | 32 /* CalendarUnitHour */ | 64 /* CalendarUnitMinute */ | 128 /* CalendarUnitSecond */;
return 4 /* NSCalendarUnit.CalendarUnitYear */ | 8 /* NSCalendarUnit.CalendarUnitMonth */ | 16 /* NSCalendarUnit.CalendarUnitDay */ | 32 /* NSCalendarUnit.CalendarUnitHour */ | 64 /* NSCalendarUnit.CalendarUnitMinute */ | 128 /* NSCalendarUnit.CalendarUnitSecond */;
}

@@ -160,3 +160,3 @@ }

if (action.launch) {
notificationActionOptions = 4 /* Foreground */;
notificationActionOptions = 4 /* UNNotificationActionOptions.Foreground */;
}

@@ -173,3 +173,3 @@ if (action.type === 'input') {

});
const notificationCategory = UNNotificationCategory.categoryWithIdentifierActionsIntentIdentifiersOptions(categoryIdentifier, actions, [], 1 /* CustomDismissAction */);
const notificationCategory = UNNotificationCategory.categoryWithIdentifierActionsIntentIdentifiersOptions(categoryIdentifier, actions, [], 1 /* UNNotificationCategoryOptions.CustomDismissAction */);
content.categoryIdentifier = categoryIdentifier;

@@ -280,3 +280,3 @@ UNUserNotificationCenter.currentNotificationCenter().getNotificationCategoriesWithCompletionHandler((categories) => {

const center = UNUserNotificationCenter.currentNotificationCenter();
center.requestAuthorizationWithOptionsCompletionHandler(4 /* Alert */ | 1 /* Badge */ | 2 /* Sound */, (granted, error) => resolve(granted));
center.requestAuthorizationWithOptionsCompletionHandler(4 /* UNAuthorizationOptions.Alert */ | 1 /* UNAuthorizationOptions.Badge */ | 2 /* UNAuthorizationOptions.Sound */, (granted, error) => resolve(granted));
}

@@ -291,3 +291,3 @@ else {

});
const types = UIApplication.sharedApplication.currentUserNotificationSettings.types | 4 /* Alert */ | 1 /* Badge */ | 2 /* Sound */;
const types = UIApplication.sharedApplication.currentUserNotificationSettings.types | 4 /* UIUserNotificationType.Alert */ | 1 /* UIUserNotificationType.Badge */ | 2 /* UIUserNotificationType.Sound */;
const settings = UIUserNotificationSettings.settingsForTypesCategories(types, null);

@@ -454,3 +454,3 @@ UIApplication.sharedApplication.registerUserNotificationSettings(settings);

payload: JSON.parse(notificationResponse.notification.request.content.userInfo.valueForKey('payload')),
foreground: this.receivedInForeground || UIApplication.sharedApplication.applicationState === 0 /* Active */,
foreground: this.receivedInForeground || UIApplication.sharedApplication.applicationState === 0 /* UIApplicationState.Active */,
event,

@@ -472,6 +472,6 @@ response,

if (notification.request.content.userInfo.valueForKey('forceShowWhenInForeground') || notification.request.content.userInfo.valueForKey('priority')) {
completionHandler(1 /* Badge */ | 2 /* Sound */ | 4 /* Alert */);
completionHandler(1 /* UNNotificationPresentationOptions.Badge */ | 2 /* UNNotificationPresentationOptions.Sound */ | 4 /* UNNotificationPresentationOptions.Alert */);
}
else {
completionHandler(1 /* Badge */ | 2 /* Sound */);
completionHandler(1 /* UNNotificationPresentationOptions.Badge */ | 2 /* UNNotificationPresentationOptions.Sound */);
}

@@ -478,0 +478,0 @@ }

{
"name": "@nativescript/local-notifications",
"version": "6.0.0",
"version": "6.1.0",
"description": "The Local Notifications plugin allows your app to show notifications when the app is not running.",

@@ -36,2 +36,6 @@ "main": "index",

"email": "oss@nativescript.org"
},
{
"name": "Alberto González",
"url": "https://github.com/agonper"
}

@@ -45,4 +49,6 @@ ],

"dependencies": {
"@nativescript-community/perms": "^2.3.0",
"@nativescript/shared-notification-delegate": "~1.0.0"
}
},
"types": "./index.d.d.ts"
}

@@ -10,10 +10,28 @@ # @nativescript/local-notifications

The Local Notifications plugin allows your app to show notifications when the app is not running.
A plugin that allows your app to show notifications when the app is not running.
Just like remote push notifications, but a few orders of magnitude easier to set up.
## Installation
## Contents
From the command prompt go to your app's root folder and execute:
* [Installation](#installation)
* [Usage](#usage)
* [Importing](#importing)
* [Requesting For Permissions](#requesting-for-permissions)
* [Scheduling A Notification](#scheduling-a-notification)
* [Getting IDs Of All The Scheduled Notifications](#getting-ids-of-all-the-scheduled-notifications)
* [Cancelling A Scheduled Notification](#cancelling-a-scheduled-notification)
* [Listening to A Notification Tap Event](#listening-to-a-notification-tap-event)
* [API](#api)
* [schedule()](#schedule)
* [ScheduleOptions](#scheduleoptions)
* [NotificationAction](#notificationaction)
* [addOnMessageReceivedCallback()](#addonmessagereceivedcallback)
* [addOnMessageClearedCallback()](#addonmessageclearedcallback)
* [getScheduledIds()](#getscheduledids)
* [cancel()](#cancel)
* [cancelAll()](#cancelall)
* [requestPermission()](#requestpermission)
* [hasPermission()](#haspermission)
#### NativeScript 7+:
## Installation

@@ -24,17 +42,9 @@ ```cli

#### NativeScript prior to 7:
## Usage
```cli
tnpm install nativescript-local-notifications@4.2.1
```
If, on iOS 10+, notifications are not being received or the method `addOnMessageReceivedCallback` is not invoked, you can try wiring to the [UNUserNotificationCenterDelegate](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc)
<!-- TODO: UNUserNotificationCenterDelegate Example -->
### Importing
## Setup
### Since plugin version 3.0.0
Add this so for iOS 10+ we can do some wiring (set the iOS `UNUserNotificationCenter.delegate`, to be precise).
Not needed if your app loads the plugin on startup anyway.
You'll know you need this if on iOS 10+ notifications are not received by your app or `addOnMessageReceivedCallback` is not invoked... better safe than sorry, though!
```typescript
```ts
// either

@@ -44,63 +54,16 @@ import { LocalNotifications } from '@nativescript/local-notifications';

import * as LocalNotifications from '@nativescript/local-notifications';
// then use it as:
LocalNotifications.hasPermission();
```
### Since plugin version 6.0.0
### Requesting For Permissions
Both iOS and Android have to register their delegates and lifecycle callbacks respectively. Hence, if your app does not load this plugin at startup you will have to add the following to your app's `app.ts`/`main.ts` file:
Automatically, the [schedule()](#schedule) method prompts the user for the permission. For the manual permission request, use the [requestPermission](#requestpermission) method:
```typescript
import '@nativescript/local-notifications';
// ... Bootstrap application
// then use it as:
LocalNotifications.requestPermission();
```
### Scheduling A Notification
## Plugin API
To schedule a notificaton, call the [schedule()](#schedule) method and pass it an array of the notification objects with the [ScheduleOptions](#scheduleoptions) properties.
### schedule
On iOS you need to ask permission to schedule a notification.
You can have the `schedule` funtion do that for you automatically (the notification will be scheduled in case the user granted permission), or you can manually invoke `requestPermission` if that's your thing.
You can pass several options to this function, everything is optional:
| option | description |
|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `id` | A number so you can easily distinguish your notifications. Will be generated if not set. |
| `title` | The title which is shown in the statusbar. Default not set. |
| `subtitle` | Shown below the title on iOS, and next to the App name on Android. Default not set. All android and iOS >= 10 only. |
| `body` | The text below the title. If not provided, the subtitle or title (in this order or priority) will be swapped for it on iOS, as iOS won't display notifications without a body. Default not set on Android, `' '` on iOS, as otherwise the notification won't show up at all. |
| `color` | Custom color for the notification icon and title that will be applied when the notification center is expanded. (**Android Only**) |
| `bigTextStyle` | Allow more than 1 line of the body text to show in the notification centre. Mutually exclusive with `image`. Default `false`. (**Android Only**) |
| `groupedMessages` | An array of at most 5 messages that would be displayed using android's notification [inboxStyle](https://developer.android.com/reference/android/app/Notification.InboxStyle.html). Note: The array would be trimmed from the top if the messages exceed five. Default not set |
| `groupSummary` | An [inboxStyle](https://developer.android.com/reference/android/app/Notification.InboxStyle.html) notification summary. Default empty |
| `ticker` | On Android you can show a different text in the statusbar, instead of the `body`. Default not set, so `body` is used. |
| `at` | A JavaScript Date object indicating when the notification should be shown. Default not set (the notification will be shown immediately). |
| `badge` | On iOS (and some Android devices) you see a number on top of the app icon. On most Android devices you'll see this number in the notification center. Default not set (0). |
| `sound` | Notification sound. For custom notification sound, copy the file to `App_Resources/iOS` and `App_Resources/Android/src/main/res/raw`. Set this to "default" (or do not set at all) in order to use default OS sound. Set this to `null` to suppress sound. |
| `interval` | Set to one of `second`, `minute`, `hour`, `day`, `week`, `month`, `year`, number (in days) if you want a recurring notification. |
| `icon` | On Android you can set a custom icon in the system tray. Pass in `res://filename` (without the extension) which lives in `App_Resouces/Android/drawable` folders. If not passed, we'll look there for a file named `ic_stat_notify.png`. By default the app icon is used. Android < Lollipop (21) only (see `silhouetteIcon` below). |
| `silhouetteIcon` | Same as `icon`, but for Android >= Lollipop (21). Should be an alpha-only image. Defaults to `res://ic_stat_notify_silhouette`, or the app icon if not present. |
| `image` | _URL_ (`http..`) of the image to use as an expandable notification image. On Android this is mutually exclusive with `bigTextStyle`. |
| `thumbnail` | Custom thumbnail/icon to show in the notification center (to the right) on Android, this can be either: `true` (if you want to use the `image` as the thumbnail), a resource URL (that lives in the `App_Resouces/Android/drawable` folders, e.g.: `res://filename`), or a http URL from anywhere on the web. (**Android Only**). Default not set. |
| `ongoing` | Default is (`false`). Set whether this is an `ongoing` notification. Ongoing notifications cannot be dismissed by the user, so your application must take care of canceling them. (**Android Only**) |
| `channel` | Default is (`Channel`). Set the channel name for Android API >= 26, which is shown when the user longpresses a notification. (**Android Only**) |
| `forceShowWhenInForeground` | Default is `false`. Set to `true` to always show the notification. Note that on iOS < 10 this is ignored (the notification is not shown), and on newer Androids it's currently ignored as well (the notification always shows, per platform default). |
| `priority` | Default is `0`. Will override `forceShowWhenInForeground` if set. This can be set to `2` for Android "heads-up" notifications. See [#114](https://github.com/EddyVerbruggen/nativescript-local-notifications/issues/114) for details. |
| `actions` | Add an array of `NotificationAction` objects (see below) to add buttons or text input to a notification. |
| `notificationLed` | Enable the notification LED light on Android (if supported by the device), this can be either: `true` (if you want to use the default color), or a custom color for the notification LED light (if supported by the device). (**Android Only**). Default not set. |
#### `NotificationAction`
| option | description |
|---------------|------------------------------------------------------------------------------------------------------------------------|
| `id` | An id so you can easily distinguish your actions. |
| `type` | Either `button` or `input`. |
| `title` | The label for `type` = `button`. |
| `launch` | Launch the app when the action completes. This will only work in apps targeting Android 11 or lower (target SDK < 31). |
| `submitLabel` | The submit button label for `type` = `input`. |
| `placeholder` | The placeholder text for `type` = `input`. |
```js

@@ -136,7 +99,99 @@ LocalNotifications.schedule([

### Notification icons (Android)
### Getting IDs Of All The Scheduled Notifications
To get the IDs of all the scheduled notifications, call the [getScheduledIds()](#getscheduledids) method:
```ts
LocalNotifications.getScheduledIds().then((ids: number[]) => {
console.log("ID's: " + ids);
});
```
### Cancelling a Scheduled Notification
To cancel a scheduled notification, use the [cancel()](#cancel) method.
```ts
LocalNotifications.cancel(5).then((foundAndCanceled: boolean) => {
if (foundAndCanceled) {
console.log("OK, it's gone!");
} else {
console.log('No ID 5 was scheduled');
}
});
```
### Listening to A Notification Tap Event
Tapping a notification in the notification center will launch your app.
Note that on iOS it will even be triggered when your app is in the foreground and a notification is received.
To handle a notification tap, call the [addMessageReceivedCallback()](#addonmessagereceivedcallback) method and pass it a callback function. The callback receives a notification object of type [ReceivedNotification](#receivednotification).
```js
LocalNotifications.addOnMessageReceivedCallback((notification) => {
console.log('ID: ' + notification.id);
console.log('Title: ' + notification.title);
console.log('Body: ' + notification.body);
}).then(() => {
console.log('Listener added');
});
```
## API
### schedule()
```ts
scheduledNotificationsIDs: Array<number> = await LocalNotifications.schedule(scheduleOptions)
```
Schedules the specified [scheduleOptions](#scheduleoptions) notification(s), if the user has granted the permission. If the user has not been prompted for the permission, it prompts and schedule the notifcation(s) if permission is granted. For manual permission request, use the [requestPermission()](#requestpermission) method.
---
#### ScheduleOptions
| Property | Type | Description
|:----------|:------------|:------------
| `id` | `number` | _Optional_: A unique notification identifier. Will be generated if not set. |
| `title` | `string` | _Optional_: The title that is shown in the statusbar. |
| `subtitle` | `string` | _Optional_: Shown below the title on iOS, and next to the App name on Android. Default not set. All android and iOS `>= 10` only. |
| `body` | `string` | _Optional_: The text below the title. If not provided, the subtitle or title (in this order or priority) will be swapped for it on iOS, as iOS won't display notifications without a body. Default not set on Android, `' '` on iOS, as otherwise the notification won't launch. |
| `color` | `string` | _Optional_: (`Android-only`)Custom color for the notification icon and title that will be applied when the notification center is expanded. |
| `bigTextStyle` | `boolean` | _Optional_: (`Android-only`)Allow more than 1 line of the body text to show in the notification centre. Mutually exclusive with `image`. Default `false`. |
| `groupedMessages` | `Array<string>` | _Optional_: An array of at most 5 messages that would be displayed using android's notification [inboxStyle](https://developer.android.com/reference/android/app/Notification.InboxStyle.html). Note: The array would be trimmed from the top if the messages exceed five. Default not set . |
| `groupSummary` | `string` | _Optional_: An [inboxStyle](https://developer.android.com/reference/android/app/Notification.InboxStyle.html) notification summary. Default empty |
| `ticker` | `string` | _Optional_: On Android you can show a different text in the statusbar, instead of the `body`. Default not set, so `body` is used. |
| `at` | `Date` | _Optional_: A JavaScript Date object indicating when the notification should be shown. Default not set (the notification will be shown immediately). |
| `badge` | `boolean` |_Optional_: On iOS (and some Android devices) you see a number on top of the app icon. On most Android devices you'll see this number in the notification center. Default not set (0). |
| `sound` | `string` |_Optional_: Notification sound. For custom notification sound, copy the file to `App_Resources/iOS` and `App_Resources/Android/src/main/res/raw`. Set this to "default" (or do not set at all) in order to use default OS sound. Set this to `null` to suppress sound. |
| `interval` | `ScheduleInterval` | _Optional_: Sets to one of `second`, `minute`, `hour`, `day`, `week`, `month`, `year`, number (in days) if you want a recurring notification. |
| `icon` | `string` |_Optional_: On Android you can set a custom icon in the system tray. Pass in `res://filename` (without the extension) which lives in `App_Resouces/Android/drawable` folders. If not passed, we'll look there for a file named `ic_stat_notify.png`. By default the app icon is used. Android < Lollipop (21) only (see `silhouetteIcon` below). See [icon and silhouetteIcon Options (Android-only)](#icon-and-silhouetteicon-options-android-only) for more details |
| `silhouetteIcon` | `string` |_Optional_: Same as `icon`, but should be an alpha-only image and will be used in Android >= Lollipop (21). Defaults to `res://ic_stat_notify_silhouette`, or the app icon if not present. See [icon and silhouetteIcon Options (Android-only)](#icon-and-silhouetteicon-options-android-only) for more details |
| `image` | `string` |_Optional_: A url of the image to use as an expandable notification image. On Android this is mutually exclusive with `bigTextStyle`. |
| `thumbnail` | `boolean` \| `string` | _Optional_: (`Android-only`)Custom thumbnail/icon to show in the notification center (to the right) on Android, this can be either: `true` (if you want to use the `image` as the thumbnail), a resource URL (that lives in the `App_Resouces/Android/drawable` folders, e.g.: `res://filename`), or a http URL from anywhere on the web. Default not set. |
| `ongoing` | `boolean` |_Optional_: (`Android-only`) Sets whether the notification is `ongoing`. Ongoing notifications cannot be dismissed by the user, so your application must take care of canceling them. |
| `channel` | `string` |_Optional_: Sets the channel name for `Android API >= 26`, which is shown when the user longpresses a notification. Default is `Channel`. |
| `forceShowWhenInForeground` | `boolean` | _Optional_: Indicates whether to always show the notification. On iOS < 10 this is ignored (the notification is not shown), and on newer Androids it's currently ignored as well (the notification always shows, per platform default). |
| `priority` | `number` |_Optional_: Overrides `forceShowWhenInForeground` if set. This can be set to `2` for Android `"heads-up"` notifications. See [#114](https://github.com/EddyVerbruggen/nativescript-local-notifications/issues/114) for details. Default is `0`. |
| `actions` | [NotificationAction](#notificationaction) | _Optional_: An array of [NotificationAction](#notificationaction) objects for adding buttons or text input to a notification with which the use can interact. |
| `notificationLed` | `boolean` \| [Color](https://docs.nativescript.org/api-reference/classes/color) |_Optional_: (`Android Only`) Indicates whether to enable the notification LED light on Android (if supported by the device), this can be either: `true` (if you want to use the default color), or a custom color for the notification LED light (if supported by the device).Default not set. |
#### NotificationAction
| Property | Type | Description
|:----------|:------------|:------------
| `id` |`string` | An id so you can easily distinguish your actions.
| `type` | `'button' \| 'input'`| The type of the view.
| `title` | `string` | _Optional_: The label for `type` = `button`.
| `launch` | `boolean` | _Optional_: Launch the app when the action completes. This will only work in apps targeting Android 11 or lower (target SDK < 31).
| `submitLabel` | `string` | _Optional_: The submit button label for `type` = `input`.
| `placeholder` | `string` | _Optional_: The placeholder text for `type` = `input`.
| `choices` | `Array<string>` | _Optional_: (`Android-only`) For `type` = `'input'`
| `editable` | `boolean` | _Optional_: (`Android-only`) For `type` = `'input'`. Defaults to `true`
### icon and silhouetteIcon Options (Android-only)
These options default to `res://ic_stat_notify` and `res://ic_stat_notify_silhouette` respectively, or the app icon if not present.
`silhouetteIcon` should be an alpha-only image and will be used in Android >= Lollipop (21).

@@ -157,15 +212,31 @@ [These are the official icon size guidelines](https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html),

### addOnMessageReceivedCallback
### addOnMessageReceivedCallback()
Tapping a notification in the notification center will launch your app.
But what if you scheduled two notifications and you want to know which one the user tapped?
```js
LocalNotifications.addOnMessageReceivedCallback((notification: ReceivedNotification) => {
//Handle the received notification
}).then(() => {
console.log('Listener added');
});
```
Responds to a notification tap event.
Use this function to have a callback invoked when a notification was used to launch your app.
Note that on iOS it will even be triggered when your app is in the foreground and a notification is received.
#### ReceivedNotification
| Property | Type | Description
|:---------|:-----|:---------
|`id`|`number`| _Optional_: The notification id.
| `foreground` | `boolean` | _Optional_: Whether the app was in foreground when the notification was received
| `title` | `string` | _Optional_: The notification title.
| `body` | `string` | _Optional_: The notification body.
| `event` | `string` | _Optional_: Whether the response was through a `button` or an `input`
| `response` | `string` | _Optional_: The user's response to the notification, either what they input in the text field or the option chosen from the button.
| `payload` | `any` | _Optional_: The data sent to the user with the notification
---
### addOnMessageClearedCallback()
```js
LocalNotifications.addOnMessageReceivedCallback((notification) => {
console.log('ID: ' + notification.id);
console.log('Title: ' + notification.title);
console.log('Body: ' + notification.body);
LocalNotifications.addOnMessageClearedCallback((notification: ReceivedNotification) => {
//Handle the received notification
}).then(() => {

@@ -175,9 +246,10 @@ console.log('Listener added');

```
Responds to a notification clear event.
See [ReceivedNotification](#receivednotification) for more info.
### getScheduledIds
---
### getScheduledIds()
If you want to know the ID's of all notifications which have been scheduled, do this:
```js
LocalNotifications.getScheduledIds().then((ids) => {
LocalNotifications.getScheduledIds().then((ids: number[]) => {
console.log("ID's: " + ids);

@@ -187,19 +259,24 @@ });

### cancel
Returns the ids of all the scheduled notifications.
If you want to cancel a previously scheduled notification (and you know its ID), you can cancel it:
---
### cancel()
```js
LocalNotifications.cancel(5 /* the ID */).then((foundAndCanceled) => {
LocalNotifications.cancel(id).then((foundAndCanceled: boolean) => {
if (foundAndCanceled) {
console.log("OK, it's gone!");
//
} else {
console.log('No ID 5 was scheduled');
//
}
});
```
Cancels the scheduled notification with the specified id.
### cancelAll
| Parameter | Type | Description
|:----------|:------|:-----------
|`id` | `number` | The of the scheduled notification to be cancelled.
If you just want to cancel all previously scheduled notifications, do this:
---
### cancelAll()

@@ -210,11 +287,7 @@ ```js

### requestPermission
Cancels all the scheduled notifications.
On Android you don't need permission, but on iOS you do. Android will simply return true.
---
### requestPermission()
If the `requestPermission` or `schedule` function previously ran the user has already been prompted to grant permission.
If the user granted permission this function returns `true`, but if he denied permission this function will return `false`,
since an iOS can only request permission once. In which case the user needs to go to the iOS settings app and manually
enable permissions for your app.
```js

@@ -226,8 +299,8 @@ LocalNotifications.requestPermission().then((granted) => {

### hasPermission
Requests for the user's permissions for the app to send her notifications.
If the permission has already been granted, it returns `true`. Otherwise, it returns `false`.
On Android you don't need permission, but on iOS you do. Android will simply return true.
---
### hasPermission()
If the `requestPermission` or `schedule` functions previously ran you may want to check whether or not the user granted permission:
```js

@@ -238,1 +311,3 @@ LocalNotifications.hasPermission().then((granted) => {

```
Checks if the application has the permission to notify the user.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc