How to send Notifications from Admin App to User App using Firebase Database

4
237
Hello everyone, today I’m going to teach you how you  send Background Notifications from your Admin Application to the User Application using Firebase Database. This can be an easy method to pass informations to the users of your app.

Unfortunately, this can’t be done using only Sketchware, you’ll need a third party Java editing Software called XML Editor Pro. With XML Editor Pro, you can edit the source code and Manifest of your Sketchware app directly, if you don’t have XML Editor Pro, you can get in the Google Play Store through this link.

So I suppose you must have gotten the necessary tools for this function which are Sketchware and XML Editor Pro, now we can head back to the deal of the day.

The first thing to do is to create two new Applications and link them both to an a single Firebase app by putting the same credentials to both of the App, you can learn more about it in this previous article. Also make sure to use the same Firebase Database location in both for this to work.

After linking both the Admin and User apps to Firebase, now open your Admin Application and place two EditText and a Button. You can optionally set the hints of the EditText to “title” and “description”.

 

  • Now create a new Firebase component, make sure to add a data location that’ll still be used to receive in the user Application  and  move to the onClick event of the button you added and add these fews blocks that is in the screenshot below.
Sketchware and Firebase Tutorial - HelpGurus

Now we are done with the Admin App, let’s move to the user Application.

In the User Application, just add only two buttons which will be used to Start receiving notifications and stop receiving notifications. In the Start button onClick event, copy the below code and paste it using an ASD block

How to use Notifications in Sketchware - HelpGurus
//Starts Background Service Intent intent = new Intent(MainActivity.this, notiservice.class); startService(intent);

 

  • For the ‘Stop Notifications’ button, place this in the onClick event.

 

//Stops Background Service Intent intent = new Intent(MainActivity.this, notiservice.class); stopService(intent);

 

  • Now run your app for 5seconds and stop it. Minimize Sketchware and open XML Editor Pro and then click on the icon that looks like ‘a pen in a book’ ( check the screenshot below ) and then click on the FAB to add a new project.

 

  • Select your ‘User’ project and click on ‘Editor Studio’ and press start.
Firebase cloud messaging in Sketchware
  • Now we head to the AndroidManifest and register the service, place this codes right above the DebugActivity just as shown in the screenshots.

/*Place this right above your .DebugActivity*/
<service android:name=”.notiservice”></service>

How to send Notifications from Admin App to User App using Firebase Database - Sketchware Tutorials - HelpGurus
  • After that, create a new java class by clicking the ‘+’ icon and name it notiservice, now open notiservice.java file and replace everything in the file with this codes
package com.yourpackage.name; import androidx.annotation.*; import android.app.*; import android.os.*; import android.view.*; import android.view.View.*; import android.widget.*; import android.content.*; import android.graphics.*; import android.media.*; import android.net.*; import android.text.*; import android.util.*; import android.webkit.*; import android.animation.*; import android.view.animation.*; import java.util.*; import java.text.*; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.ValueEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.GenericTypeIndicator; import com.google.firebase.database.ChildEventListener; import java.util.HashMap; public class notiservice extends Service { private FirebaseDatabase _firebase = FirebaseDatabase.getInstance(); private String NotiMessage = ""; private String NotiTitle = ""; private int num = 0; private DatabaseReference Database = _firebase.getReference("notifications"); private ChildEventListener _Database_child_listener; public IBinder onBind(Intent arg0) { return null;} public void onCreate() { _CreateChannel(); com.google.firebase.FirebaseApp.initializeApp(this); _Database_child_listener = new ChildEventListener() { @Override public void onChildAdded(DataSnapshot _param1, String _param2) { GenericTypeIndicator> _ind = new GenericTypeIndicator>() {}; final String _childKey = _param1.getKey(); final HashMap _childValue = _param1.getValue(_ind); NotiMessage = _childValue.get("message").toString(); NotiTitle = _childValue.get("title").toString(); Intent intent = new Intent(notiservice.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(notiservice.this, 0, intent, 0); androidx.core.app.NotificationCompat.Builder builder = new androidx.core.app.NotificationCompat.Builder(notiservice.this, "id 1") .setSmallIcon(R.drawable.app_icon) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentTitle(NotiTitle) .setContentText(NotiMessage) .setPriority(androidx.core.app.NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .setAutoCancel(true); androidx.core.app.NotificationManagerCompat notificationManager = androidx.core.app.NotificationManagerCompat.from(notiservice.this); notificationManager.notify(num, builder.build()); num++; } @Override public void onChildChanged(DataSnapshot _param1, String _param2) { GenericTypeIndicator> _ind = new GenericTypeIndicator>() {}; final String _childKey = _param1.getKey(); final HashMap _childValue = _param1.getValue(_ind); } @Override public void onChildMoved(DataSnapshot _param1, String _param2) { } @Override public void onChildRemoved(DataSnapshot _param1) { GenericTypeIndicator> _ind = new GenericTypeIndicator>() {}; final String _childKey = _param1.getKey(); final HashMap _childValue = _param1.getValue(_ind); } @Override public void onCancelled(DatabaseError _param1) { final int _errorCode = _param1.getCode(); final String _errorMessage = _param1.getMessage(); } }; Database.addChildEventListener(_Database_child_listener); } private void _CreateChannel () { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = "Background Services"; String description = "Notification channel"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("id 1", name, importance); channel.setDescription(description); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } }
How to send Notifications from Admin App to User App using Firebase Database - Sketchware Tutorials - HelpGurus

Change “com.yourpackage.name” to your package name, save your changes and then press compile and quickly minimize back to Sketchware and run the project.

NOTE: You must get a ‘successfully installed’ toast, if you don’t, minimize back to XML Editor and compile the files again and run it in Sketchware.

Now open the two apps and try sending a notification from your Admin Panel and you’ll see that it is working.

Firebase cloud messaging in Android
Our Result

Did you like this article?  just comment below and share your results..
If you got any error or Compile log don’t hesitate to comment below or contact us and we’ll reply in less than 3hrs.
Thanks for reading and have a nice day coding.

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here