Sparc.Notifications.Azure
6.0.11
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Sparc.Notifications.Azure --version 6.0.11
NuGet\Install-Package Sparc.Notifications.Azure -Version 6.0.11
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sparc.Notifications.Azure" Version="6.0.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sparc.Notifications.Azure --version 6.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sparc.Notifications.Azure, 6.0.11"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sparc.Notifications.Azure as a Cake Addin #addin nuget:?package=Sparc.Notifications.Azure&version=6.0.11 // Install Sparc.Notifications.Azure as a Cake Tool #tool nuget:?package=Sparc.Notifications.Azure&version=6.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Sparc.Notifications.Azure
The Sparc.Notifications.Azure
project enables integration between Sparc projects and Azure Notification Hubs, with fallbacks for Web Push.
Getting Started
Step 1: Install the Package
- Install the
Sparc.Notifications.Azure
NuGet package to your Sparc Features project:
> dotnet add package Sparc.Notifications.Azure
- Add the following line of code to Startup.cs to register the library's services:
services.AddAzurePushNotifications(Configuration.GetSection("Notifications"));
Step 2: Set Up Your Platforms
Android
- Create a project in Firebase: https://firebase.google.com/docs/projects/learn-more#project-id
- Add an Android App to your Firebase project and download the
google-services.json
file, then add it to your Sparc MAUI platform project. - In Firebase Console, switch to the Cloud Messaging tab and copy the Server Key.
- Add the following entries to your Sparc MAUI .csproj file (reference):
<ItemGroup>
<GoogleServicesJson Include="google-services.json" />
</ItemGroup>
- Add the following entries to your Sparc MAUI
AndroidManifest.xml
file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
Web
- Add a Web Push Key Pair in the Cloud Messaging section of your Firebase project.
- Add the necessary JavaScript to your Sparc Web index.html file (reference). Example:
<body>
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js'
// Add Firebase products that you want to use
import { getMessaging, getToken, onMessage } from 'https://www.gstatic.com/firebasejs/9.6.10/firebase-messaging.js'
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Cloud Messaging and get a reference to the service
const messaging = getMessaging(app);
// Get registration token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
const messaging = getMessaging();
getToken(messaging, { vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then((currentToken) => {
if (currentToken) {
// Send the token to your server and update the UI if necessary
// ...
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// ...
});
</script>
</body>
- Add an empty
firebase-messaging-sw.js
file to the root of your domain (ie. the wwwroot folder of your Sparc Web project). - Set up your app to handle messages that come in when your app is in the foreground (background messages are handled automatically by the browser):
// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
// `messaging.onBackgroundMessage` handler.
onMessage(messaging, (payload) => {
console.log('Message received. ', payload);
// ...
});
Step 3: Set Up Azure Notification Hubs
- Create a Notification Hub in Azure and copy the
DefaultFullSharedAccessSignature
connection string. - In the Google/FCM settings, copy the API key from step 3 in the Android section above.
- In the Apple settings, copy the API key from step 3 in the Apple section above.
- Add the following section to your Sparc Web / Sparc MAUI projects' appsettings.json file:
{
"Notifications": {
"ConnectionString": "[The DefaultFullSharedAccessSignature from Step 1 above]",
"HubName": "[The name of your Azure Hub]"
}
}
Step 4: Register the User's Devices in the Backend
- Inject
AzureNotificationService
into an appropriate Feature (i.e. a feature that registers users and/or devices).
public RegisterDevice(AzureNotificationService notifications) => Notifications = notifications;
- Call the
RegisterAsync
method on theAzureNotificationService
to register the device with Azure Notification Hubs, passing in the token received from the client.
// userId is your app's identifier for the user
// device is an injectable object of type Device that automatically pulls device and push token info from each Sparc platform
await notifications.RegisterAsync(userId, device);
Step 5: Send Messages
- Inject
AzureNotificationService
into the Feature.
public NotifyUser(AzureNotificationService notifications) => Notifications = notifications;
Create your message using the
Message
class fromSparc.Notifications.Azure
. This class represents a generic message that will be automatically translated into Android, iOS, and Web push formats for Firebase and APS.Send your message using the
SendAsync
function.
Message message = new("Time to wake up!", "Wake up, sleepyhead. It's time to face the new day!");
// Send to a specific user
await notifications.SendAsync(userId, message);
// Send to a specific device
await notifications.SendAsync(userId, deviceId, message);
// Send to a specific group of users
await notifications.SendAsync(message, "user-group-1");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.Azure.NotificationHubs (>= 4.1.0)
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Sparc.Core (>= 6.2.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.