Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAPID WebPush #3

Merged
merged 6 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions AdsPush.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdsPushSample.Api", "sample
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdsPushSample.ConsoleApp", "samples\AdsPushSample.ConsoleApp\AdsPushSample.ConsoleApp.csproj", "{CE11B712-AD05-42CD-83C4-1183CCABB081}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdsPush.Vapid", "src\AdsPush.Vapid\AdsPush.Vapid.csproj", "{5EC17E88-BF46-4822-89AE-CC4B401128D0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AdsPushSample.VapidClient", "AdsPushSample.VapidClient", "{8D84A155-DF61-4E97-A847-9CD9049E55AE}"
ProjectSection(SolutionItems) = preProject
samples\AdsPushSample.VapidClient\icon-180.png = samples\AdsPushSample.VapidClient\icon-180.png
samples\AdsPushSample.VapidClient\icon.png = samples\AdsPushSample.VapidClient\icon.png
samples\AdsPushSample.VapidClient\index.html = samples\AdsPushSample.VapidClient\index.html
samples\AdsPushSample.VapidClient\manifest.json = samples\AdsPushSample.VapidClient\manifest.json
samples\AdsPushSample.VapidClient\sample-subscription.json = samples\AdsPushSample.VapidClient\sample-subscription.json
samples\AdsPushSample.VapidClient\service-worker.js = samples\AdsPushSample.VapidClient\service-worker.js
samples\AdsPushSample.VapidClient\splash-image.jpg = samples\AdsPushSample.VapidClient\splash-image.jpg
samples\AdsPushSample.VapidClient\splash.html = samples\AdsPushSample.VapidClient\splash.html
samples\AdsPushSample.VapidClient\splash.css = samples\AdsPushSample.VapidClient\splash.css
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -54,6 +69,10 @@ Global
{CE11B712-AD05-42CD-83C4-1183CCABB081}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE11B712-AD05-42CD-83C4-1183CCABB081}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE11B712-AD05-42CD-83C4-1183CCABB081}.Release|Any CPU.Build.0 = Release|Any CPU
{5EC17E88-BF46-4822-89AE-CC4B401128D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EC17E88-BF46-4822-89AE-CC4B401128D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EC17E88-BF46-4822-89AE-CC4B401128D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EC17E88-BF46-4822-89AE-CC4B401128D0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{33EC0E05-DEC5-499C-9B58-3648A09C03A7} = {6DCD587C-FB77-4254-B98C-8E3CBC508EF1}
Expand All @@ -62,5 +81,7 @@ Global
{95D40230-2B98-474B-8525-1C4B7BBB7337} = {6DCD587C-FB77-4254-B98C-8E3CBC508EF1}
{0C9EC007-C443-415C-B7DA-D0959E4B3292} = {27B12BE4-A809-42C0-A6DC-8306BB2206BF}
{CE11B712-AD05-42CD-83C4-1183CCABB081} = {27B12BE4-A809-42C0-A6DC-8306BB2206BF}
{5EC17E88-BF46-4822-89AE-CC4B401128D0} = {6DCD587C-FB77-4254-B98C-8E3CBC508EF1}
{8D84A155-DF61-4E97-A847-9CD9049E55AE} = {27B12BE4-A809-42C0-A6DC-8306BB2206BF}
EndGlobalSection
EndGlobal
106 changes: 87 additions & 19 deletions README-NUGET.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ using AdsPush.Extensions;
}
```


And put the following section in your in your `appsettings.[ENV].json`

```
Expand All @@ -101,7 +102,8 @@ And put the following section in your in your `appsettings.[ENV].json`
"MyApp": {
"TargetMappings": {
"Ios": "Apns",
"Android": "FirebaseCloudMessaging"
"Android": "FirebaseCloudMessaging",
"BrowserAndPwa": "VapidWebPush"
},
"Apns": {
"P8PrivateKey": "<p8 certificate string without any space and start and end tags>",
Expand All @@ -121,20 +123,26 @@ And put the following section in your in your `appsettings.[ENV].json`
"AuthProviderX509CertUrl": "<auth_provider_x509_cert_url filed in service_account.json>",
"TokenUri": "<client_x509_cert_url filed in service_account.json>",
"ClientX509CertUrl": "<token_uri filed in service_account.json>"
},
"Vapid": {
"PublicKey": "<Generated public key as string>",
"PrivateKey": "<Generated private key as string>",
"Subject": "<Subjects that used in VAPID JWT.>"
}
}
}
...
}
```

If you wish to use host/pod environment or any secret provider you can set the following environment variables.

```
AdsPush__MyApp__Apns__AppBundleIdentifier=<App slug / bundle name>
AdsPush__MyApp__Apns__EnvironmentType=<Apns Env one of Development or Production>
AdsPush__MyApp__Apns__P8PrivateKey=<p8 certificate string without any space and start and end tags>
AdsPush__MyApp__Apns__P8PrivateKeyId=<10 digit p8 certificate id. Usually a part of a downloadable certificate filename>
AdsPush__MyApp__Apns__TeamId=<Apple 10 digit team id shown in Apple Developer Membership Page>
AdsPush__MyApp__Apns__AppBundleIdentifier=<App slug or bundle name>
AdsPush__MyApp__Apns__EnvironmentType=<APNs environment type; either 'Development' or 'Production'>
AdsPush__MyApp__Apns__P8PrivateKey=<p8 certificate string without spaces and start/end tags>
AdsPush__MyApp__Apns__P8PrivateKeyId=<10-digit p8 certificate id; often part of a downloadable certificate filename>
AdsPush__MyApp__Apns__TeamId=<10-digit Apple team id shown on the Apple Developer Membership Page>
AdsPush__MyApp__FirebaseCloudMessaging__AuthProviderX509CertUrl=<auth_provider_x509_cert_url filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__AuthUri=<auth_uri filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__ClientEmail=<client_email filed in service_account.json>
Expand All @@ -146,7 +154,11 @@ AdsPush__MyApp__FirebaseCloudMessaging__ProjectId=<project_id filed in service_a
AdsPush__MyApp__FirebaseCloudMessaging__TokenUri=<client_x509_cert_url filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__Type=<type filed in service_account.json>
AdsPush__MyApp__TargetMappings__Android=FirebaseCloudMessaging
AdsPush__MyApp__TargetMappings__BrowserAndPwa=VapidWebPush
AdsPush__MyApp__TargetMappings__Ios=Apns
AdsPush__MyApp__Vapid__PrivateKey=<Generated private key as string>
AdsPush__MyApp__Vapid__PublicKey=<Generated public key as string>
AdsPush__MyApp__Vapid__Subject=<Subjects that used in VAPID JWT>

```

Expand Down Expand Up @@ -183,9 +195,15 @@ var firebaseSettings = new AdsPushFirebaseSettings()
//put your configurations hare.
};

var vapidSettings = new AdsPushVapidSettings()
{
//put your configurations hare.
};

var sender = builder
.ConfigureApns(apnsSettings, null)
.ConfigureFirebase(firebaseSettings, AdsPushTarget.Android)
.ConfigureVapid(vapidSettings, null)
.BuildSender();

```
Expand All @@ -196,21 +214,43 @@ When you obtain `IAdsPushSender` instance by using one the methods shown above,

```csharp

await sender.BasicSendAsync(
AdsPushTarget.Ios,
"79eb1b9e623bbca0d2b218f44a18d7b8ef59dac4da5baa9949c3e99a48eb259a",
new ()

var basicPayload = new AdsPushBasicSendPayload()
{
Title = AdsPushText.CreateUsingString("test"),
Detail = AdsPushText.CreateUsingString("detail"),
Badge = 52,
Sound = "default",
Parameters = new Dictionary<string, object>()
{
Title = AdsPushText.CreateUsingString("test"),
Detail = AdsPushText.CreateUsingString("detail"),
Badge = 52,
Sound = "default",
Parameters = new Dictionary<string, object>()
{
{"pushParam1","value1"},
{"pushParam2","value2"},
}
});
"pushParam1", "value1"
},
{
"pushParam2", "value2"
},
}
};

var apnDeviceToken = "15f6fdd0f34a7e0f46301a817536f0fb1b2ab05b09b3fae02beba2854a1a2a16";
//var apnDeviceTokenVapid = "{"endpoint:"...", "keys": {"auth":"...","p256dh":"..."}}";

await sender.BasicSendAsync(
AdsPushTarget.Ios,
apnDeviceToken,
basicPayload);

//For VAPID WebPush with multi parametere
string
endpoint = "https://fcm.googleapis.com/fcm/send/cIo6QJ4MMtQ:APA91bEGHCpZdHaUS7otb5_xU1zNWe6TAqria9phFm7M_9ZIiEyr0vXj3gRHbeIJMYvp2-SAVbgNrVvl7uBvU_VTLpIA0CLBcmqXuuEktGr0U4LVLvwWBibO68spJk7D-lr8R9zPyAXE",
p256dh = "BIjydse4Rij892SJN10xx1qbxDM6GrYXSfg7TGu90CVM1WmlTYzn_79psRqseyWdER969LGLjZmnXIhHPaKTyGE",
auth = "TkLGLzFeUU3C9SJJN6dLAA";

var subscription = VapidSubscription.FromParameters(endpoint, p256dh, auth);
await sender.BasicSendAsync(
AdsPushTarget.BrowserAndPwa,
subscription.ToAdsPushToken(),
basicPayload);

```

Expand Down Expand Up @@ -268,4 +308,32 @@ var firebaseResult = await sender
ImageUrl = ""
}
});



//Sample for VAPID WebPush
var vapidResult = await sender
.GetVapidSender()
.SendAsync(
subscription,
new VapidRequest()
{
Title = "",
Badge = "",
Message = "",
Sound = "",
Icon = "",
Image = "",
Language = "",
Silent = false,
Tag = "",
ClickAction = "",
VibratePattern = "",
Data = new Dictionary<string, string>()
{
{"param1", "value1"}
}
});


```
107 changes: 87 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<h3 align="center">AdsPush</h3>

<p style="text-align: center">
<b>AdsPush</b> is the server-side push notification library that fully supports <b>APNS (Apple Push Notification Service)</b> and <b>FCM (Firebase Cloud Messaging)</b> features and works with the most common platforms. It offers good abstraction, is easy to use, and provides complete support for advanced use cases
<b>AdsPush</b> is the server-side push notification library that fully supports <b>APNS (Apple Push Notification Service)</b>, <b>FCM (Firebase Cloud Messaging)</b> and <b>VAPID WebPush</b> features and works with the most common platforms. It offers good abstraction, is easy to use, and provides complete support for advanced use cases

<br />
<a href="https://github.com/adessoTurkey-dotNET/AdsPush/issues">Report Bug or Request Feature</a>
Expand Down Expand Up @@ -124,7 +124,8 @@ And put the following section in your in your `appsettings.[ENV].json`
"MyApp": {
"TargetMappings": {
"Ios": "Apns",
"Android": "FirebaseCloudMessaging"
"Android": "FirebaseCloudMessaging",
"BrowserAndPwa": "VapidWebPush"
},
"Apns": {
"P8PrivateKey": "<p8 certificate string without any space and start and end tags>",
Expand All @@ -144,20 +145,26 @@ And put the following section in your in your `appsettings.[ENV].json`
"AuthProviderX509CertUrl": "<auth_provider_x509_cert_url filed in service_account.json>",
"TokenUri": "<client_x509_cert_url filed in service_account.json>",
"ClientX509CertUrl": "<token_uri filed in service_account.json>"
},
"Vapid": {
"PublicKey": "<Generated public key as string>",
"PrivateKey": "<Generated private key as string>",
"Subject": "<Subjects that used in VAPID JWT.>"
}
}
}
...
}
```

If you wish to use host/pod environment or any secret provider you can set the following environment variables.

```
AdsPush__MyApp__Apns__AppBundleIdentifier=<App slug / bundle name>
AdsPush__MyApp__Apns__EnvironmentType=<Apns Env one of Development or Production>
AdsPush__MyApp__Apns__P8PrivateKey=<p8 certificate string without any space and start and end tags>
AdsPush__MyApp__Apns__P8PrivateKeyId=<10 digit p8 certificate id. Usually a part of a downloadable certificate filename>
AdsPush__MyApp__Apns__TeamId=<Apple 10 digit team id shown in Apple Developer Membership Page>
AdsPush__MyApp__Apns__AppBundleIdentifier=<App slug or bundle name>
AdsPush__MyApp__Apns__EnvironmentType=<APNs environment type; either 'Development' or 'Production'>
AdsPush__MyApp__Apns__P8PrivateKey=<p8 certificate string without spaces and start/end tags>
AdsPush__MyApp__Apns__P8PrivateKeyId=<10-digit p8 certificate id; often part of a downloadable certificate filename>
AdsPush__MyApp__Apns__TeamId=<10-digit Apple team id shown on the Apple Developer Membership Page>
AdsPush__MyApp__FirebaseCloudMessaging__AuthProviderX509CertUrl=<auth_provider_x509_cert_url filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__AuthUri=<auth_uri filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__ClientEmail=<client_email filed in service_account.json>
Expand All @@ -169,7 +176,11 @@ AdsPush__MyApp__FirebaseCloudMessaging__ProjectId=<project_id filed in service_a
AdsPush__MyApp__FirebaseCloudMessaging__TokenUri=<client_x509_cert_url filed in service_account.json>
AdsPush__MyApp__FirebaseCloudMessaging__Type=<type filed in service_account.json>
AdsPush__MyApp__TargetMappings__Android=FirebaseCloudMessaging
AdsPush__MyApp__TargetMappings__BrowserAndPwa=VapidWebPush
AdsPush__MyApp__TargetMappings__Ios=Apns
AdsPush__MyApp__Vapid__PrivateKey=<Generated private key as string>
AdsPush__MyApp__Vapid__PublicKey=<Generated public key as string>
AdsPush__MyApp__Vapid__Subject=<Subjects that used in VAPID JWT>

```

Expand Down Expand Up @@ -206,9 +217,15 @@ var firebaseSettings = new AdsPushFirebaseSettings()
//put your configurations hare.
};

var vapidSettings = new AdsPushVapidSettings()
{
//put your configurations hare.
};

var sender = builder
.ConfigureApns(apnsSettings, null)
.ConfigureFirebase(firebaseSettings, AdsPushTarget.Android)
.ConfigureVapid(vapidSettings, null)
.BuildSender();

```
Expand All @@ -219,21 +236,43 @@ When you obtain `IAdsPushSender` instance by using one the methods shown above,

```csharp

await sender.BasicSendAsync(
AdsPushTarget.Ios,
"79eb1b9e623bbca0d2b218f44a18d7b8ef59dac4da5baa9949c3e99a48eb259a",
new ()

var basicPayload = new AdsPushBasicSendPayload()
{
Title = AdsPushText.CreateUsingString("test"),
Detail = AdsPushText.CreateUsingString("detail"),
Badge = 52,
Sound = "default",
Parameters = new Dictionary<string, object>()
{
Title = AdsPushText.CreateUsingString("test"),
Detail = AdsPushText.CreateUsingString("detail"),
Badge = 52,
Sound = "default",
Parameters = new Dictionary<string, object>()
{
{"pushParam1","value1"},
{"pushParam2","value2"},
}
});
"pushParam1", "value1"
},
{
"pushParam2", "value2"
},
}
};

var apnDeviceToken = "15f6fdd0f34a7e0f46301a817536f0fb1b2ab05b09b3fae02beba2854a1a2a16";
//var apnDeviceTokenVapid = "{"endpoint:"...", "keys": {"auth":"...","p256dh":"..."}}";

await sender.BasicSendAsync(
AdsPushTarget.Ios,
apnDeviceToken,
basicPayload);

//For VAPID WebPush with multi parametere
string
endpoint = "https://fcm.googleapis.com/fcm/send/cIo6QJ4MMtQ:APA91bEGHCpZdHaUS7otb5_xU1zNWe6TAqria9phFm7M_9ZIiEyr0vXj3gRHbeIJMYvp2-SAVbgNrVvl7uBvU_VTLpIA0CLBcmqXuuEktGr0U4LVLvwWBibO68spJk7D-lr8R9zPyAXE",
p256dh = "BIjydse4Rij892SJN10xx1qbxDM6GrYXSfg7TGu90CVM1WmlTYzn_79psRqseyWdER969LGLjZmnXIhHPaKTyGE",
auth = "TkLGLzFeUU3C9SJJN6dLAA";

var subscription = VapidSubscription.FromParameters(endpoint, p256dh, auth);
await sender.BasicSendAsync(
AdsPushTarget.BrowserAndPwa,
subscription.ToAdsPushToken(),
basicPayload);

```

Expand Down Expand Up @@ -291,6 +330,34 @@ var firebaseResult = await sender
ImageUrl = ""
}
});



//Sample for VAPID WebPush
var vapidResult = await sender
.GetVapidSender()
.SendAsync(
subscription,
new VapidRequest()
{
Title = "",
Badge = "",
Message = "",
Sound = "",
Icon = "",
Image = "",
Language = "",
Silent = false,
Tag = "",
ClickAction = "",
VibratePattern = "",
Data = new Dictionary<string, string>()
{
{"param1", "value1"}
}
});


```


Expand Down
Loading