|
1 |
| -# @jonz94/capacitor-azure-notification-hubs |
2 |
| - |
3 |
| -Capacitor plugin to register push notifications via Azure Notification Hub. |
| 1 | +<p align="center"><br><img src="https://user-images.githubusercontent.com/236501/85893648-1c92e880-b7a8-11ea-926d-95355b8175c7.png" width="128" height="128" /></p> |
| 2 | +<h3 align="center">Azure Notification Hubs</h3> |
| 3 | +<p align="center"><strong><code>@jonz94/capacitor-azure-notification-hubs</code></strong></p> |
| 4 | +<p align="center"> |
| 5 | + Capacitor plugin to register push notifications via Azure Notification Hub. |
| 6 | +</p> |
| 7 | + |
| 8 | +<p align="center"> |
| 9 | + <a href="https://www.npmjs.com/package/@jonz94/capacitor-azure-notification-hubs"><img src="https://img.shields.io/npm/l/@jonz94/capacitor-azure-notification-hubs?style=flat-square" /></a> |
| 10 | + <a href="https://www.npmjs.com/package/@jonz94/capacitor-azure-notification-hubs"><img src="https://img.shields.io/npm/v/@jonz94/capacitor-azure-notification-hubs?style=flat-square" /></a> |
| 11 | +</p> |
4 | 12 |
|
5 | 13 | ## Install
|
6 | 14 |
|
7 |
| -```bash |
8 |
| -npm install @jonz94/capacitor-azure-notification-hubs |
| 15 | +```shell |
| 16 | +npm install @jonz94/capacitor-azure-notification-hubs @capacitor/push-notifications |
9 | 17 | npx cap sync
|
10 | 18 | ```
|
11 | 19 |
|
| 20 | +## iOS |
| 21 | + |
| 22 | +On iOS you must enable the Push Notifications capability. See [Setting Capabilities](https://capacitorjs.com/docs/v3/ios/configuration#setting-capabilities) for instructions on how to enable the capability. |
| 23 | + |
| 24 | +After enabling the Push Notifications capability, add the following to your app's `AppDelegate.swift`: |
| 25 | + |
| 26 | +```swift |
| 27 | +func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
| 28 | + NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken) |
| 29 | +} |
| 30 | + |
| 31 | +func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { |
| 32 | + NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error) |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## Android |
| 37 | + |
| 38 | +The Push Notification API uses [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) SDK for handling notifications. See [Set up a Firebase Cloud Messaging client app on Android](https://firebase.google.com/docs/cloud-messaging/android/client) 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. |
| 39 | + |
| 40 | +### Variables |
| 41 | + |
| 42 | +This plugin will use the following project variables (defined in your app's `variables.gradle` file): |
| 43 | +- `$firebaseMessagingVersion` version of `com.google.firebase:firebase-messagin` (default: `23.0.5`) |
| 44 | +- `$azureNotificationHubsVersion` version of `me.leolin:ShortcutBadger` (default: `1.1.4`) |
| 45 | +- `$androidVolleyVersion` version of `com.android.volley:volley` (default: `1.2.1`) |
| 46 | +- `$kotlinVersion` version of `org.jetbrains.kotlin:kotlin-stdlib-jdk7:` (default: `1.7.10`) |
| 47 | +- `$coreKtx` version of `androidx.core:core-ktx` (default: `1.8.0`) |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +No configuration required for this plugin. |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { Device } from '@capacitor/device'; |
| 57 | +import { PushNotifications } from '@capacitor/push-notifications'; |
| 58 | +import { AzureNotificationHubs } from '@jonz94/capacitor-azure-notification-hubs'; |
| 59 | + |
| 60 | +const addListeners = async () => { |
| 61 | + await AzureNotificationHubs.addListener('registration', token => { |
| 62 | + console.info('Registration token: ', token.value); |
| 63 | + }); |
| 64 | + |
| 65 | + await AzureNotificationHubs.addListener('registrationError', err => { |
| 66 | + console.error('Registration error: ', err.error); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +const registerNotifications = async () => { |
| 71 | + let permissionStatus = await PushNotifications.checkPermissions(); |
| 72 | + |
| 73 | + if (permissionStatus.receive === 'prompt') { |
| 74 | + permissionStatus = await PushNotifications.requestPermissions(); |
| 75 | + } |
| 76 | + |
| 77 | + if (permissionStatus.receive !== 'granted') { |
| 78 | + throw new Error('User denied permissions!'); |
| 79 | + } |
| 80 | + |
| 81 | + const { uuid } = await Device.getId(); |
| 82 | + |
| 83 | + const myDeviceTag = `${uuid}-${Date.now()}` |
| 84 | + |
| 85 | + await AzureNotificationHubs.register({ |
| 86 | + notificationHubName: 'azure-notification-hub-name', |
| 87 | + connectionString: 'my-connection-string', |
| 88 | + deviceTag: myDeviceTag, |
| 89 | + }); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
12 | 93 | ## API
|
13 | 94 |
|
14 | 95 | <docgen-index>
|
|
0 commit comments