The 2017 Ionic Developer Survey Results are in!

Local Notifications

Improve this doc

This plugin allows you to display local notifications on the device

Repo: https://github.com/katzer/cordova-plugin-local-notifications

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add de.appplant.cordova.plugin.local-notification
    $ npm install --save @ionic-native/local-notifications
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { LocalNotifications } from '@ionic-native/local-notifications';


constructor(private localNotifications: LocalNotifications) { }

...


// Schedule a single notification
this.localNotifications.schedule({
  id: 1,
  text: 'Single ILocalNotification',
  sound: isAndroid? 'file://sound.mp3': 'file://beep.caf',
  data: { secret: key }
});


// Schedule multiple notifications
this.localNotifications.schedule([{
   id: 1,
   text: 'Multi ILocalNotification 1',
   sound: isAndroid ? 'file://sound.mp3': 'file://beep.caf',
   data: { secret:key }
  },{
   id: 2,
   title: 'Local ILocalNotification Example',
   text: 'Multi ILocalNotification 2',
   icon: 'http://example.com/icon.png'
}]);


// Schedule delayed notification
this.localNotifications.schedule({
   text: 'Delayed ILocalNotification',
   at: new Date(new Date().getTime() + 3600),
   led: 'FF0000',
   sound: null
});

Instance Members

schedule(options)

Schedules a single or multiple notifications

Param Type Details
options Notification|Array.<ILocalNotification>

optional

update(options)

Updates a previously scheduled notification. Must include the id in the options parameter.

Param Type Details
options ILocalNotification

optional

clear(notificationId)

Clears single or multiple notifications

Param Type Details
notificationId any

A single notification id, or an array of notification ids.

Returns: Promise<any> Returns a promise when the notification had been cleared

clearAll()

Clears all notifications

Returns: Promise<any> Returns a promise when all notifications have cleared

cancel(notificationId)

Cancels single or multiple notifications

Param Type Details
notificationId any

A single notification id, or an array of notification ids.

Returns: Promise<any> Returns a promise when the notification is canceled

cancelAll()

Cancels all notifications

Returns: Promise<any> Returns a promise when all notifications are canceled

isPresent(notificationId)

Checks presence of a notification

Param Type Details
notificationId number

Returns: Promise<boolean>

isScheduled(notificationId)

Checks is a notification is scheduled

Param Type Details
notificationId number

Returns: Promise<boolean>

isTriggered(notificationId)

Checks if a notification is triggered

Param Type Details
notificationId number

Returns: Promise<boolean>

getAllIds()

Get all the notification ids

Returns: Promise<Array<number>>

getTriggeredIds()

Get the ids of triggered notifications

Returns: Promise<Array<number>>

getScheduledIds()

Get the ids of scheduled notifications

Returns: Promise<Array<number>> Returns a promise

get(notificationId)

Get a notification object

Param Type Details
notificationId any

The id of the notification to get

Returns: Promise<ILocalNotification>

getScheduled(notificationId)

Get a scheduled notification object

Param Type Details
notificationId any

The id of the notification to get

Returns: Promise<ILocalNotification>

getTriggered(notificationId)

Get a triggered notification object

Param Type Details
notificationId

The id of the notification to get

Returns: Promise<ILocalNotification>

getAll()

Get all notification objects

Returns: Promise<Array<ILocalNotification>>

getAllScheduled()

Get all scheduled notification objects

Returns: Promise<Array<ILocalNotification>>

getAllTriggered()

Get all triggered notification objects

Returns: Promise<Array<ILocalNotification>>

registerPermission()

Register permission to show notifications if not already granted.

Returns: Promise<boolean>

hasPermission()

Informs if the app has the permission to show notifications.

Returns: Promise<boolean>

on(eventName, callback)

Sets a callback for a specific event

Param Type Details
eventName

The name of the event. Available events: schedule, trigger, click, update, clear, clearall, cancel, cancelall

callback

Call back function. All events return notification and state parameter. clear and clearall return state parameter only.

ILocalNotification

Param Type Details
id number

A unique identifier required to clear, cancel, update or retrieve the local notification in the future Default: 0

(optional)
title string

First row of the notification Default: Empty string (iOS) or the app name (Android)

(optional)
text string

Second row of the notification Default: Empty string

(optional)
every string

The interval at which to reschedule the local notification. That can be a value of second, minute, hour, day, week, month or year Default: 0 (which means that the system triggers the local notification once)

(optional)
at any

The date and time when the system should deliver the local notification. If the specified value is nil or is a date in the past, the local notification is delivered immediately. Default: now ~ new Date()

(optional)
firstAt any (optional)
badge number

The number currently set as the badge of the app icon in Springboard (iOS) or at the right-hand side of the local notification (Android) Default: 0 (which means don't show a number)

(optional)
sound string

Uri of the file containing the sound to play when an alert is displayed Default: res://platform_default

(optional)
data any

Arbitrary data, objects will be encoded to JSON string Default: null

(optional)
icon string

ANDROID ONLY Uri of the icon that is shown in the ticker and notification Default: res://icon

(optional)
smallIcon string

ANDROID ONLY Uri of the resource (only res://) to use in the notification layouts. Different classes of devices may return different sizes Default: res://ic_popup_reminder

(optional)
color string

ANDROID ONLY RGB value for the background color of the smallIcon. Default: Androids COLOR_DEFAULT, which will vary based on Android version.

(optional)
ongoing boolean

ANDROID ONLY Ongoing notifications differ from regular notifications in the following ways:

  • They are sorted above the regular notifications in the notification panel
  • They do not have an 'X' close button, and are not affected by the "Clear all" button Default: false
(optional)
led string

ANDROID ONLY ARGB value that you would like the LED on the device to blink Default: FFFFFF

(optional)
priority number

Notification priority.

(optional)

API

Native

General