CorePush 4.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package CorePush --version 4.0.2                
NuGet\Install-Package CorePush -Version 4.0.2                
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="CorePush" Version="4.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CorePush --version 4.0.2                
#r "nuget: CorePush, 4.0.2"                
#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 CorePush as a Cake Addin
#addin nuget:?package=CorePush&version=4.0.2

// Install CorePush as a Cake Tool
#tool nuget:?package=CorePush&version=4.0.2                

NuGet

.NET Core Push Notifications for Web, Android and iOS

Send notifications to:

  • iOS - Apple Push Notifications (APN)
  • Android - via Firebase Cloud Messaging (FCM)
  • Web - via Firebase Cloud Messaging (FCM)

CorePush is a simple lightweight library with minimal overhead. Send notifications to Android and Web using Firebase Cloud Messaging and iOS APN with JWT HTTP/2 API.

Installation

NuGet Package

Version 4.0.0+ requires .NET7.0. For earlier versions please use v3.1.1 of the library as it's targeting netstandard2.0, though please note, it uses legacy FCM send API. The easiest way to get started with CorePush is to use nuget package.

dotnet cli:

dotnet add package CorePush

Package Manager Console:

Install-Package CorePush

Firebase Cloud Messages for Android, iOS and Web

To start sending Firebase messages you need to have Google Project ID and JWT Bearer token. Steps to generate JWT bearer token:

  1. Enable HTTP v1 API if you haven't done it yet. Go here for instructions: https://console.firebase.google.com/project/[YOUR_GOOGLE_PROJECT_ID e.g. my-project-123456]/settings/cloudmessaging/
  2. From that page you can also go to "Manage Service Accounts". Here is the link: https://console.cloud.google.com/iam-admin/serviceaccounts and select your project.
  3. Create Service Account with "Firebase Service Management Service Agent" role.
  4. Download Service Account JSON file and use it to configure FirebaseSender.

Sending messages is very simple so long as you know the format:

var firebaseSettingsJson = await File.ReadAllTextAsync('./link/to/my-project-123345-e12345.json');
var fcm = new FirebaseSender(firebaseSettingsJson, httpClient);
await fcm.SendAsync(payload);

Useful links:

Firebase iOS notifications

If you want to use Firebase to send iOS notifications, please checkout this article: https://firebase.google.com/docs/cloud-messaging/ios/certs. The library serializes notification object to JSON using Newtonsoft.Json library and sends it to Google cloud. Here is more details on the expected payloads for FCM https://firebase.google.com/docs/cloud-messaging/concept-options#notifications. Please note, we are setting the "to" property to use device token, so you don't have to do it yourself.

Firebase Notification Payload Example

{
  "message":{
     "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvD this is DEVICE_TOKEN",
     "notification":{
       "title":"Match update",
       "body":"Arsenal goal in added time, score is now 3-0"
     },
     "android":{
       "ttl":"86400s",
       "notification"{
         "click_action":"OPEN_ACTIVITY_1"
       }
     },
     "apns": {
       "headers": {
         "apns-priority": "5",
       },
       "payload": {
         "aps": {
           "category": "NEW_MESSAGE_CATEGORY"
         }
       }
     },
     "webpush":{
       "headers":{
         "TTL":"86400"
       }
     }
   }
 }

Apple Push Notifications

To send notifications to Apple devices you have to create a publisher profile and pass settings object with necessary parameters to ApnSender constructor. Apn Sender will create and sign JWT token and attach it to every request to Apple servers:

  1. P8 private key - p8 certificate generated in itunes. Just 1 line string without spaces, ----- or line breaks.
  2. Private key id - 10 digit p8 certificate id. Usually a part of a downloadable certificate filename e.g. AuthKey_IDOFYOURCR.p8</param>
  3. Team id - Apple 10 digit team id from itunes
  4. App bundle identifier - App slug / bundle name e.g.com.mycompany.myapp
  5. Server type - Development or Production APN server
var apn = new ApnSender(settings, httpClient);
await apn.SendAsync(notification, deviceToken);

IMPORTANT: Initialize 1 ApnSender per bundle. When you send many messages at once make sure to retry the sending in case of an error. If error happens it's recommended to retry the call after 1 second delay (await Task.Delay(1000)). Apple typically doesn't like to receive too many messages and will ocasionally respond with HTTP 429. From my experiance it happens once per 1000 requests.

Please see Apple notification format examples here: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW1. Tip: To send properties like {"content-available": true} you can use Newtonsoft.Json attributes over C# properties like [JsonProperty("content-available")].

Example of notification payload

You can find expected notification formats for different types of notifications in the documentation. To make it easier to get started, here is a simple example of visible notification (the one that you'll see in phone's notification center) for iOS:

public class AppleNotification
{
    public class ApsPayload
    {
        [JsonProperty("alert")]
        public string AlertBody { get; set; }
    }

    // Your custom properties as needed

    [JsonProperty("aps")]
    public ApsPayload Aps { get; set; }
}

Use [JsonProperty("alert-type")] attribute to serialize C# properties into JSON properties with dashes.

Setup for ASP.NET Core with Dependency Injection

Both ApnSender and FirebaseSender have dependencies that need to be registered in order to enable DI.

  1. Register HttpClient in Startup.cs. This will allow injection of HttpClient into the FCM and APN senders:
services.AddHttpClient<FirebaseSender>();
services.AddHttpClient<ApnSender>();
  1. Register settings object as a singleton:

If you've added ApnSettings and FirebaseSettings into a configuration section, you can bind section directly to settings object from IConfiguration available in Startup.cs:

var section = configuration.GetSection("ApnSettings");
var settings = new AppSettings();
section.Bind(settings);

Add settings to services:

services.AddSingleton(apnSettings);
services.AddSingleton(fcmSettings);

MIT License

Copyright (c) 2020 Andrei M

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CorePush:

Package Downloads
ISDS.ServiceExtender

Package Description

Indiko.Blocks.Messaging.PushNotification

Building Blocks Messaging Push Notification for Google FCN and Apple APNS

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.2.1 7,328 9/27/2024
4.2.0 982 9/20/2024
4.1.0 148,408 3/10/2023
4.0.2 2,136 2/26/2023
4.0.1 886 2/26/2023
3.1.1 335,184 7/7/2022
3.1.0 11,561 7/4/2022
3.0.12 17,669 6/14/2022
3.0.11 15,382 5/20/2022
3.0.10 159,773 10/2/2021
3.0.4 113,791 2/17/2021
3.0.3 3,514 2/12/2021
3.0.2 1,295 2/9/2021
3.0.1 41,641 9/25/2020
3.0.0 8,942 8/29/2020
2.1.2 21,012 7/23/2020
2.1.1 27,946 4/3/2020
2.1.0 1,536 2/27/2020
2.0.3 8,555 2/20/2020
2.0.2 1,728 2/7/2020
2.0.1 19,697 10/30/2019
2.0.0 8,957 11/29/2018
1.1.0 1,019 8/15/2018
1.0.1 2,364 10/22/2017
1.0.0 1,206 10/22/2017

v4.0.1
- Use .NET 7.0 as a target framework
- Use System.Text.Json as a default serializer
- [BREAKING CHANGE] Firebase Messaging uses HTTP v1 API
- [BREAKING CHANGE] Different configuration is required because of the HTTP v1
     
v3.1.x
- Memory optimizations
- Ability to change FCM and APN base URL for testing purposes
     
v3.0.11 - v3.0.12
- Package information update

v3.0.5 - v3.0.10
- Minor code improvements
- Documentation update
- Package icon

v3.0.4
- Bugfixes with FcmSender authorization and serialization
- CorePush.Tester added to make testing easier

v3.0.3
- Cancellation tokens added to the interface with CancellationToken.None by default

v3.0.2
- Reverted Portable.BouncyCastle so that the lib can work in shared envs (like Azure App Service)
- Apple certificate cleanup added
- Minor code improvements added

v3.0.1
- Removed Portable.BouncyCastle and System.Security.Cryptography.Cng dependency

v3.0.0
- Added ApnSettings and FcmSettings
- HttpClient has to be injected for FcmSender and ApnSender
- Both ApnSender and FcmSender are not IDisposable anymore as HttpClient is injected and managed outside
- ApnSender JWT token expiration is managed according to Apple documentation