
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
How to use firebase messaging in android application?
This example demonstrate How to use firebase messaging in android application
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to src/MainActivity.java
<?xml version = "1.0" encoding = "utf-8"?> import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 3 − Add the following code to src/ MyFirebaseMessagingService.java
<?xml version = "1.0" encoding = "utf-8"?> import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.support.v4.app.NotificationCompat; import android.support.v4.content.ContextCompat; import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import org.json.JSONObject; import java.util.Map; public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onNewToken(String s) { Log.e("NEW_TOKEN", s); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> params = remoteMessage.getData(); JSONObject object = new JSONObject(params); Log.e("JSON_OBJECT", object.toString()); String NOTIFICATION_CHANNEL_ID = "sairam"; long pattern[] = {0, 1000, 500, 1000}; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH); notificationChannel.setDescription(""); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setVibrationPattern(pattern); notificationChannel.enableVibration(true); mNotificationManager.createNotificationChannel(notificationChannel); } // to diaplay notification in DND Mode if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) { NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID); channel.canBypassDnd(); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); notificationBuilder.setAutoCancel(true) .setColor(ContextCompat.getColor(this, R.color.colorAccent)) .setContentTitle(getString(R.string.app_name)) .setContentText(remoteMessage.getNotification().getBody()) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_background) .setAutoCancel(true); mNotificationManager.notify(1000, notificationBuilder.build()); } }
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –
Click here to download the project code
- Related Articles
- How to create firebase account for android application?
- How to use Font Awesome in Native Android Application?
- Adding Firebase to Android App
- Android : How to upload image on Firebase Storage?
- How to use SQLite database with an Android application?
- How to use Font Awesome in Native Android Application using Kotlin?
- How to detect application heap size in android?
- How to Launch an application from another application on Android
- How to get android application version name?
- How to close an Android application gracefully?
- How to print application base apk path in android?
- How to play YouTube video in my Android Application?
- How to add manifest permission to an application in Android?
- How to get android application first installation date?
- How to get android application last update information?

Advertisements