How to make push notification using our custom API in android

Updated 6 July 2023

Save

Android push notification is a service which notifies you that has any new data come or not in your smart device.  

Android push notification is a service used by developers to send data to Android smartphones.

There are several different methods that can be used to implement Android push messages, the standard way is GCM (Google Cloud Messaging) but the alternatives are everywhere.

So here, We are using simply an alarmManager to make a request to the server after an interval and our custom API to make a response to android smartphones.


1. Make your pendingIntent and set alarm

This is your primary activity named MainActivity.java, Here we have to set pendingIntent where pendingIntent is the instance of the Intent that starts the Activity when a user touches the push notification.

In above block of code,

alarmIntent – is an object of Intent.
PendingIntent – is the instance of the Intent that starts the Activity when a user touches the push notification.
AlarmManager – This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.
PendingIntent.FLAG_UPDATE_CURRENT – Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity, getBroadcast, and getService.

Here we have set the interval of 1000 * 60 * 60 * 2. Means It will run after every 2 hr.

2. Make your BroadcastReceiver class

In step 2, We are making a BroadcastReceiver class named AlarmReceiver.java.

Here, we send a request to the server and server send a response to the client (mobile device) and show to the form of push notification on smartphones.

In above code,

BroadcastReceiver – broadcast receiver who will intercept communication and notify us that something is working in our mobile. It is of 2 types:

NotificationCompat.Builder – Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts.

3. Register your broadcast receiver class in AndroidManifest.xml

An application listens for specific broadcast intents by registering a broadcast receiver in AndroidManifest.xml file.

Paste this xml code inside your <application> tag.

 

 

 

 

 

 

 

 

 

 

 

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home