Giter Club home page Giter Club logo

jonz94 / capacitor-azure-notification-hubs Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 1.31 MB

⚡️ Capacitor plugin to register push notifications via Azure Notification Hub.

Home Page: https://www.npmjs.com/package/@jonz94/capacitor-azure-notification-hubs

License: BSD Zero Clause License

Ruby 8.95% Java 10.09% Objective-C 6.34% Swift 33.24% JavaScript 4.00% TypeScript 18.98% Kotlin 18.40%
android azure-notification-hubs capacitor capacitor-android capacitor-ios capacitor-plugin ios push-notifications

capacitor-azure-notification-hubs's Introduction


Azure Notification Hubs

@jonz94/capacitor-azure-notification-hubs

Capacitor plugin to register push notifications via Azure Notification Hub.

Install

npm install @jonz94/capacitor-azure-notification-hubs @capacitor/push-notifications
npx cap sync

iOS

On iOS you must enable the Push Notifications capability. See Setting Capabilities for instructions on how to enable the capability.

After enabling the Push Notifications capability, add the following lines to ios/App/App/AppDelegate.swift:

import UIKit
import Capacitor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

+    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
+        NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
+    }
+
+    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
+        NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
+    }

    // ...

}

Android

The Push Notification API uses Firebase Cloud Messaging SDK for handling notifications. See Set up a Firebase Cloud Messaging client app on Android and follow the instructions for creating a Firebase project and registering your application. There is no need to add the Firebase SDK to your app or edit your app manifest - the Push Notifications provides that for you. All that is required is your Firebase project's google-services.json file added to the module (app-level) directory of your app.

Variables

This plugin will use the following project variables (defined in your app's variables.gradle file):

  • firebaseMessagingVersion version of com.google.firebase:firebase-messaging (default: 23.1.2)
  • azureNotificationHubsVersion version of com.microsoft.azure:notification-hubs-android-sdk-fcm (default: 1.1.4)
  • androidVolleyVersion version of com.android.volley:volley (default: 1.2.1)
  • androidxCoreKTXVersion version of androidx.core:core-ktx (default: 1.10.0)
  • kotlin_version version of org.jetbrains.kotlin:kotlin-stdlib (default: 1.8.20)

Configuration

No configuration required for this plugin.

Usage

import { Device } from '@capacitor/device';
import { PushNotifications } from '@capacitor/push-notifications';
import { AzureNotificationHubs } from '@jonz94/capacitor-azure-notification-hubs';

const addListeners = async () => {
  await AzureNotificationHubs.addListener('registration', token => {
    console.log('Registration token: ', token.value);
  });

  await AzureNotificationHubs.addListener('registrationError', err => {
    console.error('Registration error: ', err.error);
  });
}

const registerNotifications = async () => {
  let permissionStatus = await PushNotifications.checkPermissions();

  if (permissionStatus.receive === 'prompt') {
    permissionStatus = await PushNotifications.requestPermissions();
  }

  if (permissionStatus.receive !== 'granted') {
    throw new Error('User denied permissions!');
  }

  const { identifier } = await Device.getId();

  const myDeviceTag = `${identifier}-${Date.now()}`

  await AzureNotificationHubs.register({
    notificationHubName: 'azure-notification-hub-name',
    connectionString: 'my-connection-string',
    deviceTag: myDeviceTag,
  });
}

API

register(...)

register(info: RegisterInfo) => Promise<void>

Register the app to receive push notifications.

This method will trigger the 'registration' event with the push token or 'registrationError' if there was a problem. It does not prompt the user for notification permissions, use PushNotifications.requestPermissions() from @capacitor/push-notifications first.

Param Type
info RegisterInfo

Since: 1.0.0


addListener('registration', ...)

addListener(eventName: 'registration', listenerFunc: (token: Token) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

Called when the push notification registration finishes without problems.

Provides the push notification token.

Param Type
eventName 'registration'
listenerFunc (token: Token) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

Since: 1.0.0


addListener('registrationError', ...)

addListener(eventName: 'registrationError', listenerFunc: (error: RegistrationError) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

Called when the push notification registration finished with problems.

Provides an error with the registration problem.

Param Type
eventName 'registrationError'
listenerFunc (error: RegistrationError) => void

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

Since: 1.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

Remove all native listeners for this plugin.

Since: 1.0.0


Interfaces

RegisterInfo

Prop Type Description Since
notificationHubName string The notification hub name. 1.0.0
connectionString string The connection string. 1.0.0
deviceTag string The device tag. 1.0.0

PluginListenerHandle

Prop Type
remove () => Promise<void>

Token

Prop Type Description Since
value string On iOS it contains the APNS token. On Android it contains the FCM token. 1.0.0

RegistrationError

Prop Type Description Since
error string Error message describing the registration failure. 1.0.0

Changelog

See CHANGELOG.md.

License

See LICENSE.

capacitor-azure-notification-hubs's People

Contributors

jonz94 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

capacitor-azure-notification-hubs's Issues

Error on iOS (setup push channel)

Hello, push notifications are working on Android but not on iOS for me. I have implemented the plugin as mentioned in the documentation. When I register the plugin for iOS I get the following error:

2023-09-12 16:26:53.765769+0200 App[10917:116230] Push notifications authorization was granted.
⚡️  To Native ->  AzureNotificationHubs addListener 125145408
⚡️  To Native ->  AzureNotificationHubs addListener 125145409
2023-09-12 16:26:55.753247+0200 App[10917:116007] Error while creating installation: The operation couldn’t be completed. (WindowsAzureMessaging error -1.)
{
    Error = "You have to setup Push Channel before save installation";
}

feat: upgrade to capacitor 6

Discussed in #6

Originally posted by Tramb-dev April 27, 2024
Hi,

As Capacitor 6 was recently released, I want to know if you plan to make this great plugin compatible with this new version of Capacitor?

Best regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.