Android Push Notifications in Sketchware with FCM (Firebase Cloud Messaging)

How to send and receive notification messages or data messages with FCM in Sketchware ?

POISSAC
4 min readOct 6, 2021

--

Step 3

Configure the app manifest file

Now create custom import

So import these libs
import java.text.Normalizer;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;

Ok Good !

So now we gonna use COMMAND block for injecting/removing some code on the AndroidManifest.xml we’ll explain further COMMAND block feature in next articles
I’ll give some screenshots :

Copy these codes here
Cmd 1 : reference = android:priority="-500"

Cmd 2 : reference = com.google.firebase.messaging.FirebaseMessagingService

Codes = android:directBootAware="true"

Cmd 3 : reference =com.google.firebase.messaging.FirebaseMessagingService

Codes =.FCMService

The notification background color and icon can be set like this:

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_launcher" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/darkred" />

Add these meta-data inside the <application> tag

Go check Android manifest source code for know the changes.

Now lets add some custom permissions of your project.
Here’s the complete list of permissions needed :

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.REBOOT"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<permission android:name="<your-package-name>.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="<your-package-name>.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

Step 4

Access the device registration token

I’m using custom block created by myself for achieve this task.

We have to generate the device registration token, it’ll allow us to send notification message to a specific device.

Or if you prefer using Add Source directly here is the code behind this block :

FirebaseInstanceId.getInstance().getInstanceId()
 .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (task.isSuccessful()) {

token = task.getResult().getToken();

}

else {

SketchwareUtil.showMessage(getApplicationContext(), task.getException() != null ? task.getException().getMessage() : "");

}
}
});

We could use the device registration token for send notification to a specific user.

Step 5

Custom notification handling

Create a class with the name MyFirebaseMessagingService.java

It contains mainly three important sections:

  1. Create a class MyFirebaseMessagingService and extend it with FirebaseMessagingService
  2. onMessageReceived is the method that is called when a message is received. It has one param remoteMessage Object representing the message received from Firebase Cloud Messaging. So, override this method into the class created in the above step
  3. sendNotification is the method used to generate & handle notifications.

Let’s now take a look at the sendNotification() function

--

--

POISSAC

Poissac is a blog dedicated to high-quality technology articles : www.poissac.com