|
| 1 | + |
| 2 | + |
| 3 | +### AdsPush |
| 4 | + |
| 5 | +**AdsPush** is the server-side push notification library supports fully APNS(Apple Push Notification Service) and FCM (Firebase Cloud Messaging) |
| 6 | + |
| 7 | +- [NuGet](https://www.nuget.org/packages/AdsPush) |
| 8 | +- [Report Bug or Request Feature](https://github.com/adessoTurkey-dotNET/AdsPush/issues) |
| 9 | +- [Contect Me Via Mail](mailto:mail@anildursunsenel.com?subject=AdsPush) |
| 10 | +- [Contect Me Via Linkedin](https://www.linkedin.com/in/anıl-dursun-şenel) |
| 11 | + |
| 12 | +# Features |
| 13 | +1. Abstraction sender works with APNS & FCM |
| 14 | +2. Full support for all functionality platform specific parameters. |
| 15 | +3. Support up-to-date recommended APIs. |
| 16 | +4. Easy to use. |
| 17 | +5. Advanced configuration options |
| 18 | + |
| 19 | + |
| 20 | +# Get It Started |
| 21 | +1. Install AdsPush by using the following command |
| 22 | + |
| 23 | + ` dotnet add package AdsPush ` from dotnet cli |
| 24 | + |
| 25 | + or ` Install-Package AdsPush ` from package console |
| 26 | + |
| 27 | +2. Decide using platform (APNS or/and FCM) and get the required configuration files from the portal. |
| 28 | +3. Apply the following instructions to configure and send notifications. |
| 29 | + |
| 30 | +# Configuration |
| 31 | +You have two easy options to be able configure AdsPush |
| 32 | + |
| 33 | +1. Using Microsoft Dependency Injection (recommended) |
| 34 | + - Using default configuration provider (Microsoft Options Pattern) |
| 35 | + - Using custom configuration provider. |
| 36 | +2. Using direct sender instance. |
| 37 | + |
| 38 | +### Microsoft Dependency Injection |
| 39 | + |
| 40 | +Microsoft Dependency Injection is Microsoft's IOC library coming with .NET Core. AdsPush supports using MDI to be able to manage your push configuration and sending operations. |
| 41 | + |
| 42 | +If you're sing .NET 6 or newer version in `Program.cs` |
| 43 | + |
| 44 | +```csharp |
| 45 | + |
| 46 | +using AdsPush.Extensions; |
| 47 | + |
| 48 | +var builder = WebApplication.CreateBuilder(args); |
| 49 | +//Option 1:From configuration |
| 50 | +builder.Services.AddAdsPush(this.Congiguration); |
| 51 | + |
| 52 | +//Option 2:From Action |
| 53 | +builder.Services.AddAdsPush(options => |
| 54 | +{ |
| 55 | + //Your configurations |
| 56 | +}); |
| 57 | + |
| 58 | +//Option 3:From custom provider that is implementation of IAdsPushConfigurationProvider interface. |
| 59 | +builder.Services.AddAdsPush<MyProivdr>(); |
| 60 | +``` |
| 61 | +If you're sing .NET 5 or any .NET Core version in `Startup.cs` |
| 62 | + |
| 63 | +```csharp |
| 64 | +using AdsPush.Extensions; |
| 65 | +... |
| 66 | + |
| 67 | + public override void ConfigureServices(IServiceCollection services) |
| 68 | + { |
| 69 | + //your code... |
| 70 | + |
| 71 | + //Option 1:From configuration |
| 72 | + services.AddAdsPush(this.Congiguration); |
| 73 | + |
| 74 | + //Option 2:From Action |
| 75 | + services.AddAdsPush(options => |
| 76 | + { |
| 77 | + //Your configurations |
| 78 | + }); |
| 79 | + |
| 80 | + //Option 3:From custom provider that is implementation of IAdsPushConfigurationProvider interface. |
| 81 | + services.AddAdsPush<MyProivdr>(); |
| 82 | + |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +And put the following section in your in your `appsettings.[ENV].json` |
| 87 | + |
| 88 | +```json |
| 89 | +{ |
| 90 | + "Logging": { |
| 91 | + ... |
| 92 | + }, |
| 93 | + "AdsPush": { |
| 94 | + "MyApp": { |
| 95 | + "TargetMappings": { |
| 96 | + "Ios": "Apns", |
| 97 | + "Android": "FirebaseCloudMessaging" |
| 98 | + }, |
| 99 | + "Apns": { |
| 100 | + "P8PrivateKey": "<p8 certificate string without any space and start and end tags>", |
| 101 | + "P8PrivateKeyId": "<10 digit p8 certificate id. Usually a part of a downloadable certificate filename>", |
| 102 | + "TeamId": "<Apple 10 digit team id shown in Apple Developer Membership Page>", |
| 103 | + "AppBundleIdentifier": "<App slug / bundle name>", |
| 104 | + "EnvironmentType": "<Apns Env one of Development or Production>" |
| 105 | + }, |
| 106 | + "FirebaseCloudMessaging": { |
| 107 | + "Type":"<type filed in service_account.json>", |
| 108 | + "ProjectId":"<project_id filed in service_account.json>", |
| 109 | + "PrivateKey": "<private_key filed in service_account.json>", |
| 110 | + "PrivateKeyId": "<private_key_id filed in service_account.json>", |
| 111 | + "ClientId": "<client_id filed in service_account.json>", |
| 112 | + "ClientEmail": "<client_email filed in service_account.json>", |
| 113 | + "AuthUri": "<auth_uri filed in service_account.json>", |
| 114 | + "AuthProviderX509CertUrl": "<auth_provider_x509_cert_url filed in service_account.json>", |
| 115 | + "TokenUri": "<client_x509_cert_url filed in service_account.json>", |
| 116 | + "ClientX509CertUrl": "<token_uri filed in service_account.json>" |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + ... |
| 121 | +} |
| 122 | +``` |
| 123 | +If you wish to use host/pod environment or any secret provider you can set the following environment variables. |
| 124 | + |
| 125 | +```config |
| 126 | +AdsPush__MyApp__Apns__AppBundleIdentifier=<App slug / bundle name> |
| 127 | +AdsPush__MyApp__Apns__EnvironmentType=<Apns Env one of Development or Production> |
| 128 | +AdsPush__MyApp__Apns__P8PrivateKey=<p8 certificate string without any space and start and end tags> |
| 129 | +AdsPush__MyApp__Apns__P8PrivateKeyId=<10 digit p8 certificate id. Usually a part of a downloadable certificate filename> |
| 130 | +AdsPush__MyApp__Apns__TeamId=<Apple 10 digit team id shown in Apple Developer Membership Page> |
| 131 | +AdsPush__MyApp__FirebaseCloudMessaging__AuthProviderX509CertUrl=<auth_provider_x509_cert_url filed in service_account.json> |
| 132 | +AdsPush__MyApp__FirebaseCloudMessaging__AuthUri=<auth_uri filed in service_account.json> |
| 133 | +AdsPush__MyApp__FirebaseCloudMessaging__ClientEmail=<client_email filed in service_account.json> |
| 134 | +AdsPush__MyApp__FirebaseCloudMessaging__ClientId=<client_id filed in service_account.json> |
| 135 | +AdsPush__MyApp__FirebaseCloudMessaging__ClientX509CertUrl=<token_uri filed in service_account.json> |
| 136 | +AdsPush__MyApp__FirebaseCloudMessaging__PrivateKey=<private_key filed in service_account.json> |
| 137 | +AdsPush__MyApp__FirebaseCloudMessaging__PrivateKeyId=<private_key_id filed in service_account.json> |
| 138 | +AdsPush__MyApp__FirebaseCloudMessaging__ProjectId=<project_id filed in service_account.json> |
| 139 | +AdsPush__MyApp__FirebaseCloudMessaging__TokenUri=<client_x509_cert_url filed in service_account.json> |
| 140 | +AdsPush__MyApp__FirebaseCloudMessaging__Type=<type filed in service_account.json> |
| 141 | +AdsPush__MyApp__TargetMappings__Android=FirebaseCloudMessaging |
| 142 | +AdsPush__MyApp__TargetMappings__Ios=Apns |
| 143 | +
|
| 144 | +``` |
| 145 | + |
| 146 | +Now, you can easily use wia DI as the. following. |
| 147 | + |
| 148 | +```csharp |
| 149 | +private readonly IAdsPushSender _pushSender; |
| 150 | +public MyService( |
| 151 | + IAdsPushSenderFactory adsPushSenderFactory) |
| 152 | +{ |
| 153 | + this._pushSender = adsPushSenderFactory.GetSender("MyApp"); |
| 154 | +} |
| 155 | + |
| 156 | +``` |
| 157 | + |
| 158 | +### Using Sender Instance |
| 159 | + |
| 160 | +The following lines of codes can be used without any DI configuration. |
| 161 | + |
| 162 | +```csharp |
| 163 | + |
| 164 | +using AdsPush; |
| 165 | +using AdsPush.Abstraction; |
| 166 | +using AdsPush.Abstraction.Settings; |
| 167 | + |
| 168 | +var builder = new AdsPushSenderBuilder(); |
| 169 | +var apnsSettings = new AdsPushAPNSSettings() |
| 170 | +{ |
| 171 | + //put your configurations hare. |
| 172 | +}; |
| 173 | + |
| 174 | +var firebaseSettings = new AdsPushFirebaseSettings() |
| 175 | +{ |
| 176 | + //put your configurations hare. |
| 177 | +}; |
| 178 | + |
| 179 | +var sender = builder |
| 180 | + .ConfigureApns(apnsSettings, null) |
| 181 | + .ConfigureFirebase(firebaseSettings, AdsPushTarget.Android) |
| 182 | + .BuildSender(); |
| 183 | + |
| 184 | +``` |
| 185 | + |
| 186 | +## Sending notifications |
| 187 | + |
| 188 | +When you obtain `IAdsPushSender` instance by using one the methods shown above, you're ready to send notification. The following sample code can be used trigger a basic notification request. |
| 189 | + |
| 190 | +```csharp |
| 191 | + |
| 192 | +await sender.BasicSendAsync( |
| 193 | + AdsPushTarget.Ios, |
| 194 | + "79eb1b9e623bbca0d2b218f44a18d7b8ef59dac4da5baa9949c3e99a48eb259a", |
| 195 | + new () |
| 196 | + { |
| 197 | + Title = AdsPushText.CreateUsingString("test"), |
| 198 | + Detail = AdsPushText.CreateUsingString("detail"), |
| 199 | + Badge = 52, |
| 200 | + Sound = "default", |
| 201 | + Parameters = new Dictionary<string, object>() |
| 202 | + { |
| 203 | + {"pushParam1","value1"}, |
| 204 | + {"pushParam2","value2"}, |
| 205 | + } |
| 206 | + }); |
| 207 | + |
| 208 | +``` |
| 209 | + |
| 210 | +If you wish to access whole supported parameters of the related platform, the following methods can be helpful. |
| 211 | + |
| 212 | +```csharp |
| 213 | + |
| 214 | + |
| 215 | +//sample for Apns |
| 216 | +var apnsResult = await sender |
| 217 | + .GetApnsSender() |
| 218 | + .SendAsync( |
| 219 | + new APNSRequest() |
| 220 | + { |
| 221 | + ApnsPayload = new() |
| 222 | + { |
| 223 | + Badge = 52, |
| 224 | + Sound = "", |
| 225 | + MutableContent = true, |
| 226 | + FilterCriteria = "", |
| 227 | + ThreadId = "", |
| 228 | + TargetContentId = "", |
| 229 | + Alert = new APNSAlert() |
| 230 | + { |
| 231 | + Title = "", |
| 232 | + Body = "", |
| 233 | + Subtitle = "" |
| 234 | + } |
| 235 | + //more... |
| 236 | + }, |
| 237 | + AdditionalParameters = new Dictionary<string, object>() |
| 238 | + { |
| 239 | + {"pushParam1", "value1" }, |
| 240 | + {"pushParam2", "value2"}, |
| 241 | + {"pushParam3", 52}, |
| 242 | + } |
| 243 | + }, |
| 244 | + "79eb1b9e623bbca0d2b218f44a18d7b8ef59dac4da5baa9949c3e99a48eb259a", |
| 245 | + Guid.NewGuid()); |
| 246 | + |
| 247 | +//sample for FCM |
| 248 | +var firebaseResult = await sender |
| 249 | + .GetFirebaseSender() |
| 250 | + .SendToSingleAsync(new Message() |
| 251 | + { |
| 252 | + Token = "", |
| 253 | + Android = new AndroidConfig() |
| 254 | + { |
| 255 | + Priority = Priority.High, |
| 256 | + }, |
| 257 | + Notification = new() |
| 258 | + { |
| 259 | + Title = "", |
| 260 | + Body = "", |
| 261 | + ImageUrl = "" |
| 262 | + } |
| 263 | + }); |
| 264 | +``` |
0 commit comments