Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Web API push to Apple/Android push notification servers

I have Chat service in .NET Core Web API, handling chat messages through Signal R when clients are online.

Also, i have mobile clients (android/apple) and i need to handle messages sent when client is in offline mode. I am aware if clients are offline/online. So, when the client is offline, i want to send message to apple/android push notification server so it can notify the device about new message(s).

Which options do i have to connect net core Web API to android/apple push notification servers? Any experiences with sending client push notifications to mobile clients from .net service?

like image 976
zlaayaa Avatar asked Oct 02 '18 14:10

zlaayaa


People also ask

Can websites send push notifications iOS?

Overview. When important, time-sensitive events occur, inform your website users with push notifications you send from your server. In Safari 16 in macOS 13 or later, Safari supports web push — push notifications that use the cross-browser Push API, Notifications API, and Service Worker standards.

Can a website send push notifications to phone?

Although we often associate web push with desktop notifications, web push notifications can also appear on tablets and Android devices (Apple iOS does not support web push). The name “web” refers to the mechanism by which notifications are sent to a user's device via a web browser (e.g. Chrome, Safari).

How do I send notifications from a website to my Android app?

For sending Notification to any android Device you can use two technology: 1) Push. 2) Pull. For Push Technology you can use GCM(Google cloud messaging). For Pull Technology you can make you application continuously keep on connecting to server and trying to fetch data if it is available from there.


2 Answers

Use CorePush lib

It's very lightweight. I use it across all my projects to send Firebase Android and Apple iOS push notifications. Useful links:

  1. NuGet package
  2. Documentation

The interface is very simple and minimalistic:

Send APN message:

var apn = new ApnSender(settings, httpClient);
await apn.SendAsync(notification, deviceToken);

Send FCM message:

var fcm = new FcmSender(settings, httpClient);
await fcm.SendAsync(deviceToken, notification);
like image 87
Andrei Avatar answered Oct 27 '22 00:10

Andrei


You need To make a POST request to https://fcm.googleapis.com/fcm/send with the following parameters Header

  • Authorization: key=YOUR_SERVER_KEY
  • Content-Type: Application/JSON

Body:

{
 "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "First Notification",
     "title": "Collapsing A"
 },
 "data" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "key_1" : "Data for key one",
     "key_2" : "Hellowww"
 }

You can get your server Token from Firebase Console. Also you need to set up APN in order for this to work on iOS. You can find More info here

like image 1
Farabi Abdelwahed Avatar answered Oct 27 '22 00:10

Farabi Abdelwahed