I have a web system (.NET MVC) for the company where I work, in which the user does some CRUD operations. Then I made an app in Android Studio which shows a recyclerview of the records that users create in the web system that I mentioned at the beginning, for this I also created a .NET MVC server. But what I need is that every time an insert is made (to the database) in the web system, a notification arrives in the mobile app.
I was reading about PUSHER, I registered and it gave me the following code:
Android client
PusherOptions options = new PusherOptions();
options.setCluster("xxx");
Pusher pusher = new Pusher("xxx", options);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange change) {
System.out.println("Estado cambio de " + change.getPreviousState() + " a " + change.getCurrentState());
}
@Override
public void onError(String message, String code, Exception e) {
System.out.println("Error de conexión! " +
"\ncode: " + code +
"\nmessage: " + message +
"\nException: " + e
);
}
}, ConnectionState.ALL);
Channel channel = pusher.subscribe("my-channel");
channel.bind("my-event", new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
System.out.println("Received event with data: " + event.toString());
}
});
And for the .NET MVC server
public class HelloWorldController : Controller
{
[HttpPost]
public async Task<ActionResult> Notificaciones()
{
var options = new PusherOptions
{
Cluster = "xxx",
Encrypted = true
};
var pusher = new Pusher(
"xxx",
"xxxx",
"xxxx",
options);
var result = await pusher.TriggerAsync(
"my-channel",
"my-event",
new { message = "hello world" });
return new HttpStatusCodeResult((int)HttpStatusCode.OK);
}
}
The truth is that I am very lost and I don't know how to use it, I read the documentation but they use it with firebase, but I have to use the bases that they have on a private server of my company. If anyone can guide me or tell me about some other way to do what I need. Thank you
What you could do in this case is to use Firebase Cloud Messaging (FCM) which is free of charge . For this you need to add Firebase to your Android project .
To add Firebase to your project you must:
google-services.json
into Android Studio in the root folder of your project.app/build.gradle
your module (app level).To send the notification to mobile devices through the FCM service you can use the Firebase Admin SDK that is available for the C# language, through this SDK you can notify mobile devices when you make a new insert.
This is an example of what could go on your server
.NET MVC
to send a notification to a device:We must take into account what @Juan mentions that every time an application is installed on Android, a Firebase token is created per device, that token must be stored and monitored to ensure that it is active, since, for example, if a person uninstalls the application and the reinstalls the token changes. In addition, you have to take into account the concept of themes that Firebase manages to send notifications to several devices, here is an example how to send a message to all devices in case you need it or you can send notifications to specific devices through their token as in the sample code.
I'm going to consider "I'm very lost" as the question, so this answer is to guide you.
From what I read in the Pusher documentation, it follows the pattern of publish-subscribe channels, where a client subscribes to a channel and a server sends messages to the channel and all subscribers to the channel receive the message.
On the other hand, you have public, private, and presence channels. The last two require the client to authenticate.
I would understand that for a corporate application you would be needing to use private channels. If the insert in the database is to be notified to a particular user, which depends on the inserted record, then you need one channel per user, on the other hand if the inserts in the database are to be communicated to all users , you could use a single channel for all users.
As for the server side, it seems to me that the integration is not at the database level but at the level of the application that does the insert. You need to add the Pusher libraries (server libraries) to the project dependencies of that application. And when the application makes an insert in the database, after committing it, it can publish the event in the corresponding channel.
It is not expressly written but it seems to me that some administration for Pusher you are going to have to set up on the server side in the database, at least for user authentication and if there are individual channels, also keep track of which channel is for which user maybe a few more things.
The documentation on the page is pretty good. Hence you need to put together a simple model to understand how the parts work before going to implement the whole project.
Regarding other ways to do it:
There are many providers that use Firebase behind the scenes (One Signal, Urban Airship, etc). Check the terms of use before deciding on one (also check the terms of use of Pusher).
Firebase-based ones use the concept of "topic" so that a client can receive notifications similar to the Pusher channel.
But unlike Pusher, individual notifications are not sent to "topics" but rather to a "token" or a group of "tokens".
The "token" is a Firebase-generated device identifier, which can change at any time.
For this reason, for individual messages, you need, on the one hand, to manage the user tokens on the server to know which one is valid for each one. On the other hand you need code in the app to send the "token" to the server to be refreshed when Firebase decides to generate a new token for the device.
And also some error handling when you send the notifications since a "token" that seems to be valid, may not be.