Skip to content

juitnow/juit-nativescript-local-notifications

Repository files navigation

Minimalistic Local Notifications for NativeScript 7

This package implements a minimalistic helper to schedule and cancel local notifications on iOS and Android.

Usage

The local notifications plugin should be easily instantiated by simply calling its constructor

import { LocalNotifications } from '@juit/nativescript-local-notifications'

const localNotifications = new LocalNotifications({
  androidIcon: 'icon_notification',
  androidColor: 0x0FF0000,
})

Scheduling notifications

A local notification can be scheduled using the schedule(...) method with a LocalNotificationRequest and a Date or the number of seconds to wait before displaying the notification:

const notificationId = await localNotifications.schedule({
  title: 'The title',
  message: 'The message for the notification...',
  data: {
    // Optional, this is a payload that will be
    // returned when the notification is opened
  }
}, 10) // schedule in 10 seconds...

The schedule(...) method will return a Promise which will resolve to the string id of the scheduled notification.

Canceling a scheduled notification

A scheduled notification can be canceled (and therefore will no longer be presented to the user) by using the cancel(...) method:

localNotifications.cancel(notificationId)

Subscribing to notifications

When a notification is opened by a user, the notification event will be emitted by the LocalNotifications instance. The usual on(...), once(...) and off(...) methods are available to manage subscription:

localNotifications.on('notification', (notification) => {
  console.log('Notification opened', notification)
})

// Notification opened: {
//   id: '0ecfd51b-6e83-46a9-8b6d-61632bf91db3',
//   title: 'The title',
//   message: 'The message for the notification...',
//   data: { ... }
// }