npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nativescript-custom-local-notifications

v1.0.3

Published

The Local Notifications plugin allows your app to show notifications when the app is not running. now with custom sounds (android only). Just like remote push notifications, but a few orders of magnitude easier to set up.

Downloads

6

Readme

NativeScript Local Notifications Plugin with custom sounds for android

The Local Notifications plugin 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.

For Custom sound, Add a folder called 'raw' in /app/App_Resources/Android and add your custom sounds.

Note

This repository is a fork of the local notifications plugin by Eddy Verbruggen (eddyverbruggen). So big thanks to Eddy Verbruggen - Team Obsessive.

Installation

From the command prompt go to your app's root folder and execute:

tns plugin add nativescript-custom-local-notifications

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. Default 0.| |title |The title which is shown in the statusbar. Default empty.| |body |The text below the title. 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 'now'.| |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 |Currently this is only used on Android where you can set this to null to suppress the sound. Default sound is the sound file located at /Appresources/raw/notify.mp3|

LocalNotifications.schedule([{
    id: 1,
    title: 'The first title',
    body: 'The first  body',
    ticker: 'The ticker',
    badge: 1,
    sound: "sound1", //sound1 from /Appresources/raw/ folder
    at: new Date(new Date().getTime() + (20 * 1000)) 
  }]).then(
      function() {
        console.log("Notification scheduled 1");
      },
      function(error) {
        console.log("scheduling error: " + error);
      }
  );
  LocalNotifications.schedule([{
    id: 1,
    title: 'The title',
    body: 'The body',
    ticker: 'The ticker',
    badge: 1,
    sound: null, // suppress sound on Android
    at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
  }]).then(
      function() {
        console.log("Notification scheduled");
      },
      function(error) {
        console.log("scheduling error: " + error);
      }
  )

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?

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.

  LocalNotifications.addOnMessageReceivedCallback(
      function (notification) {
        console.log("ID: " + notification.id);
        console.log("Title: " + notification.title);
        console.log("Body: " + notification.body);
      }
  ).then(
      function() {
        console.log("Listener added");
      }
  )

getScheduledIds

If you want to know the ID's of all notifications which have been scheduled, do this:

Note that all functions have an error handler as well (see schedule), but to keep things readable we won't repeat ourselves.

  LocalNotifications.getScheduledIds().then(
      function(ids) {
        console.log("ID's: " + ids);
      }
  )

cancel

If you want to cancel a previously scheduled notification (and you know its ID), you can cancel it:

  LocalNotifications.cancel(5 /* the ID */).then(
      function(foundAndCanceled) {
          if (foundAndCanceled) {
            console.log("OK, it's gone!");
          } else {
            console.log("No ID 5 was scheduled");
          }
      }
  )

cancelAll

If you just want to cancel all previously scheduled notifications, do this:

  LocalNotifications.cancelAll();

requestPermission

On Android you don't need permission, but on iOS you do. Android will simply return true.

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.

  LocalNotifications.requestPermission().then(
      function(granted) {
        console.log("Permission granted? " + granted);
      }
  )

hasPermission

On Android you don't need permission, but on iOS you do. Android will simply return true.

If the requestPermission or schedule functions previously ran you may want to check whether or not the user granted permission:

  LocalNotifications.hasPermission().then(
      function(granted) {
        console.log("Permission granted? " + granted);
      }
  )

Contributors