Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Push notifications in Android can be implemented using several services, with Firebase Cloud Messaging (FCM) being one of the most popular. Here's how you can implement push notifications in an Android app using FCM:
If you haven't already, add Firebase to your Android project.
In the Firebase console, go to your project settings and download the google-services.json
file. Add this file to your app module's root directory in Android Studio.
In your app's build.gradle
file, add the necessary dependencies:
implementation 'com.google.firebase:firebase-messaging:20.2.4' // Check for the latest version
Create a service that extends FirebaseMessagingService
. This service will handle the receipt of the push notifications:
public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); // Handle FCM messages here. For example, create a notification based on the message's content. if (remoteMessage.getNotification() != null) { String title = remoteMessage.getNotification().getTitle(); String body = remoteMessage.getNotification().getBody(); showNotification(title, body); } } private void showNotification(String title, String body) { // Create and show a notification } }
Register the service in the AndroidManifest.xml
:
<service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
Use the NotificationManager to display the received notifications. Make sure to create a notification channel if targeting Android Oreo (API level 26) and above.
Go to the Firebase console.
Navigate to the Cloud Messaging tab.
Click on "Send your first message" or "Send a message".
Fill in the necessary details and target the message to your app.
Click "Review" and then "Publish" to send the notification.
Now, when you run your app, it should be able to receive and display push notifications sent from the Firebase Console.
Remember: To make the most of push notifications, always ensure the content is relevant and timely. Avoid sending too many notifications to prevent users from muting them or uninstalling your app.
Android push notification example code:
// Inside your FirebaseMessagingService @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getData().size() > 0) { // Handle data payload String message = remoteMessage.getData().get("message"); sendNotification(message); } if (remoteMessage.getNotification() != null) { // Handle notification payload String title = remoteMessage.getNotification().getTitle(); String body = remoteMessage.getNotification().getBody(); sendNotification(body); } } private void sendNotification(String messageBody) { // Create and show a notification // ... }
Android notification channels and push notifications:
NotificationCompat.Builder
when creating notifications.// Creating a notification channel if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // Building a notification with a channel NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id"); // ...
Push notification libraries for Android development:
// OneSignal example dependency implementation 'com.onesignal:OneSignal:4.3.2'
Follow the documentation of the chosen library for detailed integration instructions.