Posted on

Android java app with notification from mvc using firebase cloud messaging – part 3

Notification service in Android

Now we need to create in Android the service able to manage the notification incoming from firebase and to show these to user. Follow this usefull article Working easily with FCM push notifications in Android. You’ll have a class like this.

package com.mycompany.myfirstapp;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;



//is the class that we can find in the manifest as a service

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String TAG = "MsgFirebaseServ";


    public void onMessageReceived(RemoteMessage remoteMessage) {

        String title = "";
        String body = "";
        String objectId = "";
        String objectType = "";

        if (remoteMessage.getData() != null) {
            title = remoteMessage.getData().get("title");
            body = remoteMessage.getData().get("body");
            objectId = remoteMessage.getData().get("object_id");
            objectType = remoteMessage.getData().get("objectType");

        Notification notification = new NotificationCompat.Builder(this)
        NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
        manager.notify(/*notification id*/0, notification);

Run your app on your device

Follow this instruction :

First test using your app in your device

  • Run the app on your device or emulator. The app should had created a new register id for the device and send it to mvc site. Now the device is registered.
  • Open Firebase console, go down in the menu on the left. Open Cloud Messaging session : you should find the possibility to send notification to your device starting from this page. So you can test correct developing of above class.

Send notification from mvc to firebase

For backend developers. You should create a class to manage notification to firebase. Follow the article Firebase push notifications using a .NET Backend to prepare a class to manage the notification to Firebase.

Then prepare your notification test task in a controller. Something like this :

        public Task<bool> NoteAsync()
            var device = db.RegisteredDevices.Where(d => d.ENABLED == true);
            string[] da = new string[device.Count()];
            int i = 0;
            foreach (RegDevice r in device)
                da[i] = r.ID;
            return FCMPushNotification.SendPushNotification(da,serverkey,"Title","body");

Firebase messaging, where to get Server Key?

Click the Settings (Cog wheel) icon next to your project name at the top of the new Firebase Console, as per screenshot below:

  1. Click Project settings.
  2. Click on the Cloud Messaging tab.

Final test

Call the mvc method created. You should see a notification in your device.

Android java app with notification from mvc using firebase cloud messaging – part 1

Android java app with notification from mvc using firebase cloud messaging – part 2


Posted on Leave a comment


Visual Studio allows us to publish our web site on a web server in the lan (intranet) or on an external web server, for example hosted on Azure.

In the situation of a server on the lan, you have created, through IIS, a site that resides on a certain directory, empty now, on a web server. Otherwise you have already prepared an external site.

In Visual Studio, in the right panel called “Solution Explorer” we click with the right mouse button on our project and select the “Publish” option. The “Publish” window: there are, on the left, the fourth step to do for publication.


Go to the drop down menu at the top center and choose “New Custom Profile”. Invent the name of this profile. Once inserted, you will go directly to the next step.

Connection with “Web Deploy”

Publish Metod : choose “Web Deploy”  whether you publish your site on a server on the lan, both in an external server.

Server (web server in lan) : http://myserver

Server (external, es Azure):

Site name (web server in lan) : name of the directory in which it is to be the site

Site name (external, es Azure) : es. mysite

User Name e Password (web server in lan) : put a administrator user of the server machine. If the machine is in a domain,  use a local administrator.

User Name e Password (esterno) : ftp user

Destination Url : web site address. es : http://……………..


In this tab the program reads from we.config the database connection strings. If your target site database is different, you need to specify the right connection. The publication will replace the target strings to the source strings. We will see later that there is a method to insert in the web.config further changes to the web.config of origin.


Here you can see the files that will be sent to the destination that is the new files or changed since the last submission. And you can publish your site!

web.config configuration

Our destination website may have other special features, specified in web.config, different from the starting site, as well as database connection strings. We can create a specific web.config for each deployment.

Position  the mouse on the project. In the “Properties”, find the directory “PublishProfiles” which contains previously saved data. Here you will find a .pubxml files for each of the deployment that you have created. Go to the file you need, push the right mouse button and select “Add Config Transform”.

If you go in the web.config you will see that there is an additional file named as your deployment. Let’s see some examples of what you can do with the tag that you set within the <configuration>

 <add name="MyDB"
 connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
 xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

this stringa substitutes (xdt:Transform=”SetAttributes”) the attributes of the tag named  (xdt:Locator=”Match(name)) “MyDB” with these.

<appSettings><add key="myemail" value="" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/></appSettings>

this stringa substitutes the attributes (xdt:Transform=”SetAttributes”) of the tag with key (xdt:Locator=”Match(key)) “myemai” with these.

 <mailSettings xdt:Transform="Replace">
 <smtp deliveryMethod="Network" ........................

This string transform (xdt:Transform=”Replace”) entire tag mailSettings and its content.

Posted on


Our site needs to send emails to its users, for example when the user forgets the password. To do this comes to the rescue System.Net.Mail.SmtpClient class.

This class automatically uses the SMTP server settings stored in the Web.config file. You just have to add to that file the following tags inside the configuration tag:

 <smtp deliveryMethod="Network" from="mail_from">
 <network host="my_smtp_server" port="my_smtp_server_port" userName="user_name_smtp" password="password" clientDomain="my_domain" />


In the view Account/Login , you can see, bottom, on the left, the link to Account/ForgotPassword that is the page where the user can reset his password.

In the  controller Account, in the action post  “ForgotPassword” you make operational, removing them as a comment, the lines of code dedicated to password recovery.

In the file IdentityConfig.cs, in the function

public Task SendAsync(IdentityMessage message)

and replaced to the line

return Task.FromResult(0);

with the following lines of code

var sentFrom = ""
 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
 client.EnableSsl = false;

// Create the message:
 var mail =
 new System.Net.Mail.MailMessage(sentFrom, message.Destination);

mail.Subject = message.Subject;
 mail.Body = message.Body;
 mail.IsBodyHtml = true;

// Send:
 return client.SendMailAsync(mail);

You have activated the password recovery

Posted on


The View / Shared folder contains all the visual controls that are to be displayed (used) in different parts of the site: are the old “user controls.” These controls can be called programmatically or called by the site itself automatically.

In this second case, imagine you create the “Student” model. In the View / Shared folder must exist (if not, create them) folders:

  • DisplayTemplates
  • EditorTemplates

If you enter in the 2 folders, 2 view dedicated to the display of “Student” model in two forms of viewing and editing, both named Studente.cshtml, the site will show you your custom control.

Posted on Leave a comment

ASP.NET MVC – First Controller

Visual Studio creates the Controller of Model. Let’s see how.

After creating the Model, Student.cs, in the project, in Visual Studio, position yourself on the Controllers directory. Push the right mouse button and press “Add” -> “Controller”. There appears a mask that offers you to choose the type of controller that you want to create. First on the list it is an empty controller, that is, a class that you have to write independently. The third should be a controller of type “Controller with View, using Entity Framework .” Visual Studio, starting with our Model, creates for us the Controller and view all of the CRUD operations. Choose this type.

Now appears a very important mask. From the top you are asked to choose:

  • Model Class. There is a drop-down menu that is automatically filled with all the classes in the Model (and all the classes in your project). Student choose.
  • Date Class context. You must choose the connection to the database that contains your table. The drop down menu shows all the classes in the project, derived from DbContext class,
  • Check “Generate Views” and “use a Page Layout“.
  • Controller name. Visual Studio there proposes a name one based on the Model you’ve selected. You can change the name, but let the word Controller at the end. (Eg. StudentsController)

Before having to set up the Controller, Visual Studio gives you everything you have written in the models and in DbContext is “right.” If the operation is successful now, in the Controllers directory you find the file StudentsController.cs.

Let’s examine this file. You are inside the class StudentsController derived from Controller.

public class StudentsController : Controller
 private ApplicationDbContext db = new ApplicationDbContext();

// GET: Students
 public ActionResult Index()
 return View(db.Students.ToList());

The class immediately initializes a connection to the database (db).

The first function we see is Index(). It ‘was created automatically and allow us to see the list of our students. Each function of this type is called “Action.” When, in our site we require a list of Students, we launch the Index function of the controller Students according to this path:

The Index function requires, from the connection to the database, all students (db.Students) and turns them into a list (db.Students.ToList ()). At this point sends our list of students to the view (return View (db.Students.ToList ());) As you can see, there isn’t the name of view but only a generic command “View”. The controller  searches in the list of views, for a view  called exactly like the function (Index) and … we see the students.

In the project, in the directory Views, you will find a folder Student with a list of automatically created files (we’ll analyze the view shortly). In the View directory will find the Shared directory. We can say that this directory contains all the “User Controls” ; all interface items that we intend to use in multiple pages; It contains all of the view that are used by other views. Even the one that in the past called “master page” is located here, in the form of “layout”. Look for the _Layout.cshtml file. This is the website of your graphics (we’ll analyze in detail, soon). Find  this piece of code:

<ul class="nav navbar-nav">
 <li>@Html.ActionLink("Home", "Index", "Home")</li>
 <li>@Html.ActionLink("About", "About", "Home")</li>
 <li>@Html.ActionLink("Contact", "Contact", "Home")</li>

These are the buttons you see above, when you launch your site. We need to see a new button to call the student page.

Add to this list html:

<li>@Html.ActionLink("My Student", "Index", "Students")</li>

Then we analyze the ActionLink component.

ActionLink (“Text Displayed”, “Name of Action,” Name of the Controller “). So we will have a button that when pushed will take us to page.

Try running the application and perform a login to test that everything is working.