
In the article, How to get your Android App ready to add Push Notifications, we showed you how to set up the necessary data and files in your application and admin panel to get your App ready to add this feature. Now that you’ve completed these steps, you’re ready to create a class for received push notifications.
First of all, you should add a class that extends QBFcmPushListenerService. In the example below, it is called PushListenerService.
In a callback sendPushMessage you can add custom logic for handling the received message.
For example, you can add a class to show notifications with information with static methods.
import android.app.Activity; 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.graphics.Color; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import androidx.annotation.DrawableRes; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; public class NotificationUtils { private static final String CHANNEL_ONE_ID = "com.quickblox.samples.ONE";// The id of the channel. private static final String CHANNEL_ONE_NAME = "Channel One"; public static void showNotification(Context context, Class<? extends Activity> activityClass, String title, String message, @DrawableRes int icon, int notificationId) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createChannelIfNotExist(notificationManager); } Notification notification = buildNotification(context, activityClass, title, message, icon); notificationManager.notify(notificationId, notification); } @RequiresApi(api = Build.VERSION_CODES.O) private static void createChannelIfNotExist(NotificationManager notificationManager) { if (notificationManager.getNotificationChannel(CHANNEL_ONE_ID) == null) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, importance); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.BLUE); notificationChannel.setShowBadge(true); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); notificationManager.createNotificationChannel(notificationChannel); } } private static Notification buildNotification(Context context, Class<? extends Activity> activityClass, String title, String message, @DrawableRes int icon) { Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); return new NotificationCompat.Builder(context, CHANNEL_ONE_ID) .setSmallIcon(icon) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(buildContentIntent(context, activityClass, message)) .build(); } private static PendingIntent buildContentIntent(Context context, Class<? extends Activity> activityClass, String message) { Intent intent = new Intent(context, activityClass); intent.putExtra(FcmConsts.EXTRA_FCM_MESSAGE, message); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } }
And call static method showNotification to show notifications like in the code below.
Of course, instead of App.getInstance
you should pass a Context and instead of SplashActivity.class you should pass any Activity class from your application.
notification_title is your text from String resource which you can show in a notification message in a title space. You should use a drawable icon to show a picture in a notification message.
Next, you should add the following changes in the AndroidManifest.xml file.
Register PushListenerService and register QBFcmPushInstanceIDService.
Now that you’ve implemented these steps, your App is ready to receive and display push notifications. Got more questions? Contact support.
Want to learn more about building Android Apps? Check out our other tutorials:
How to Launch Android Chat an Webrtc Video Conferencing Samples
How to Build an Android Chat Application with Kotlin using QuickBlox SDK