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.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import androidx.core.app.NotificationManagerCompat;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
//is the class that we can find in the manifest as a service
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static final String TAG = "MsgFirebaseServ";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(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)
.setContentTitle(title)
.setContentText(body)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(/*notification id*/0, notification);
}
}
Run your app on your device
Follow this instruction : https://developer.android.com/training/basics/firstapp/running-app
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 asp.net 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 asp.net 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;
i++;
}
string serverkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
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:
- Click Project settings.
- Click on the Cloud Messaging tab.
Final test
Call the asp.net mvc method created. You should see a notification in your device.
Android java app with notification from asp.net mvc using firebase cloud messaging – part 1
Android java app with notification from asp.net mvc using firebase cloud messaging – part 2
Links
- add firebase to your android project official
- Firebase console
- Setup a firebase cloud messaging client app on Android
- Part 1 : Token based authentication in ASP.NET Web API
- Firebase push notifications using a .NET Backend
- Firebase messaging, where to get Server Key?
- Java HttpsURLConnection example
- Java HttpsURLConnection example 2
- Working easily with FCM push notifications in Android
- Run your app on your device