Skip to content

dotnetdreamer/capacitor-notificationlistener

 
 

Repository files navigation

capacitor-notificationlistener

Observe android notification being posted / removed using this NotificationListenerService wrapper for capacitor.

DISCLAIMER

This plugin is quite old and untested for newer versions. It will probably not work with current versions of Android.

Installation

npm i capacitor-notificationlistener
npx cap sync

Register this plugin using add(NotificationListenerPlugin.class) in your MainActivity.java like in the following example:

// ... code ...
import ch.asinz.capacitornotificationlistener.NotificationListenerPlugin;
// ... code ...
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Put it here!
      add(NotificationListenerPlugin.class);
    }});
// ... code ...

Permissions

Add the following, contained in <application>, to your AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<service android:name="ch.asinz.capacitornotificationlistener.NotificationService"
    android:label="@string/app_name"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

<!-- https://developer.android.com/training/articles/direct-boot (LOCKED_BOOT_COMPLETED) is required if devices has lock on-->
<receiver android:directBootAware="true"
    android:name="ch.asinz.capacitornotificationlistener.StartAppOnBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT" />
        <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

Usage

Import the plugin.

import { SystemNotification, SystemNotificationListener } from 'capacitor-notificationlistener';
const sn = new SystemNotificationListener();

Start listening for notifications.

sn.startListening();

Add a listener for new notifications or the removal of notifications. Make sure you have called sn.startListening() to be able to receive notifications.

sn.addListener("notificationReceivedEvent", (info: SystemNotification) => {
    // logic ...
});
sn.addListener("notificationRemovedEvent", (info: SystemNotification) => {
    // logic ...
});

SystemNotification Interface. The anotomy of android notifications is explained here.

interface SystemNotification {
  apptitle: string;     // Title of a notifications' app
  text: string;         // Text of a notification
  textlines: string[];  // Text of a multi-line notification
  title: string;        // Title of a notification
  time: Date;           // Time when a notification was received
  package: string;      // Package-name of a notifications' app
}

Check if the App is listening for notifications. If it is not, even though sn.startListening() was called, your app doesn't have sufficient permissions to observe notifications.

sn.isListening().then((value : boolean) => {
    // logic ... 
    // example code:
    // if not listening
    if (!value)
        // ask for Permission
        sn.requestPermission()
});

Open settings so that the user can authorize your app.

sn.requestPermission();

Stop listening for notifications.

sn.stopListening();

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 66.5%
  • TypeScript 19.0%
  • Swift 5.9%
  • Ruby 3.8%
  • Objective-C 2.7%
  • JavaScript 2.1%