Index
- Introduction
- The Android Application
- Sending from Firebase Portal
- The .Net Core Service
Code on Github
Create Project on Android Studio
Setup the project
- Start a new Android Studio Project with the "Empty Activity" template

- And choose an API Level at least of 5.0 (Lollipop)

- Now if you are not, log in on your google account with Android Studio

- Then on the File->Settings menu verify the presence of the Firebase plugins, and ensure that all are enabled. And restart Android Studio

- Reopening the project a new tool will appear under Tools->Firebase. Opening it on the right choose "Setup Firebase Cloud Messaging"

- Then, click on "Connect your app to Firebase", and "Add FCM to your app". The first will open a browser instance where you should

Create a form
- First we create a constants file. The three first field are for the "IDs" of the UI items inside the data. The channel stuffs is needed to register a "notification channel" (see Create and Manage Notification Channels on developer.android.com. "After you create a notification channel, you cannot change the notification behaviors—the user has complete control at that point. Though you can still change a channel's name and description.
You should create a channel for each distinct type of notification you need to send. You can also create notification channels to reflect choices made by users of your app. For example, you can set up separate notification channels for each conversation group created by a user in a messaging app."
public class MyConstants {
static String STR_SHORT_DESC = "ShortDesc";
static String STR_INCIDENT_NO = "IncidentNo";
static String STR_DESC = "IncidentNo";
static String CHANNEL_ID = "1";
static CharSequence CHANNEL_NAME = "my_channel";
static String CHANNEL_DESCRIPTION = "My Channel";
static String TOPIC = "MyApp";
}
-
Create then a new FirebaseMessagingService class under your main package.
import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.media.RingtoneManager; import android.net.Uri; import android.util.Log; import androidx.core.app.NotificationCompat; import com.google.firebase.messaging.RemoteMessage; import java.util.Map; /** * Created by Ashish.kapoor on 04.09.2017. * Modified by Kendar on 2020.05.04 */ //Inherited from FirebaseMessagingService public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { private static final String TAG = "MyMessagingService"; public MyFirebaseMessagingService() { } //Notice the onMessageReceived Override that will receive the data and post the getData() the extra parameters map, to the app @Override public void onMessageReceived(RemoteMessage remoteMessage) { String strTitle = remoteMessage.getNotification().getTitle(); String message = remoteMessage.getNotification().getBody(); Log.d(TAG, "onMessageReceived: Message Received: \n" + "Title: " + strTitle + "\n" + "Message: " + message); sendNotification(strTitle, message, remoteMessage.getData()); } @Override public void onDeletedMessages() { } //Here goes the real juice private void sendNotification(String title, String messageBody, Map<String, String> data) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); -
//Should create a "CHANNEL" if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel mChannel = new NotificationChannel( MyConstants.CHANNEL_ID, MyConstants.CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); mChannel.setDescription(MyConstants.CHANNEL_DESCRIPTION); //mChannel.enableLights(true); //mChannel.setLightColor(Color.RED); mChannel.enableVibration(true); mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); //mChannel.setShowBadge(false); notificationManager.createNotificationChannel(mChannel); } //Retrieve the current intent Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(MyConstants.STR_INCIDENT_NO, data.get(MyConstants.STR_INCIDENT_NO)); intent.putExtra(MyConstants.STR_DESC, data.get(MyConstants.STR_DESC)); intent.putExtra(MyConstants.STR_SHORT_DESC, data.get(MyConstants.STR_SHORT_DESC)); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION); //Build and send the notification NotificationCompat.Builder notificationBuilder = new NotificationCompat .Builder(this, MyConstants.CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .setLargeIcon(BitmapFactory.decodeResource (getResources(), R.mipmap.ic_launcher)); notificationManager.notify(0, notificationBuilder.build()); } } -
Prepare now the view editing the res->layout->activity_main.xml. You should
-
- Remove the "Hello world" label
- Add the TextView with
- ID "txtIncidentNo"
- text "Incident No"
- textAppearance->textSize "36sp"
- Add the TextView with
- ID "txtShortDesc"
- text "Short Description"
- textAppearance->textSize "24sp"
- Add the TextView with
- ID "txtDesc"
- text "Description"
- textAppearance->textSize "24sp"
- Add the Button with
- ID "btnGetData"
- text "Get Data"
- textAppearance->textSize "24sp"
- commonAttributes->onClick "loadData"
-
Now the MainActivity should be changed, to receive the intent data and to apply to the main view. And, important, to subscribe to a Topic
import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private String strSDesc; private String strIncidentNo; private String strDesc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); onNewIntent(getIntent()); FirebaseMessaging.getInstance().subscribeToTopic(MyConstants.TOPIC); } @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); Bundle extras = intent.getExtras(); if (extras != null) { setContentView(R.layout.activity_main); final TextView IncidentTextView = (TextView) findViewById(R.id.txtIncidentNo); strIncidentNo = extras.getString(MyConstants.STR_INCIDENT_NO, ""); final TextView SDescTextView = (TextView) findViewById(R.id.txtShortDesc); strSDesc = extras.getString(MyConstants.STR_SHORT_DESC, ""); final TextView DescTextView = (TextView) findViewById(R.id.txtDesc); strDesc = extras.getString(MyConstants.STR_DESC, ""); IncidentTextView.setText(strIncidentNo); SDescTextView.setText(strSDesc); DescTextView.setText(strDesc); } } public void loadData(View view) { Intent browserIntent = new Intent (Intent.ACTION_VIEW, Uri.parse ("https://somebrowser?uri=incident.do?sysparm_query=number=" + strIncidentNo)); startActivity(browserIntent); } }