How to add Push Notification capability to your Android app

Nilesh Singh
Dec 18, 2017 · 7 min read
Image for post
Image for post

Everything from generating tokens to receiving and displaying notifications

<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
compile ‘com.google.firebase:firebase-messaging:10.2.6’
String token = FirebaseInstanceId.getInstance().getToken();
Tokens get refreshed every time an reinstall happens or app starts after app cache removal.
connection.setRequestMethod(“POST”);
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());dos.writeBytes(“token=” + token);
MessaginReceiver provides us with the notification data.
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
'notification' : { 'body' : 'We are done, hurray!', 'title' : 'Notifications for Android', 'icon' : 'appIcon' }
PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE, i, PendingIntent.FLAG_UPDATE_CURRENT);
<service android:name=”.MessageReceiver”>
<intent-filter>
<action android:name=”com.google.firebase.MESSAGING_EVENT” />
</intent-filter>
</service>
<service android:name=”.InstanceIdService”>
<intent-filter>
<action android:name=”com.google.firebase.INSTANCE_ID_EVENT” />
</intent-filter>
</service>
Image for post
Image for post
That’s our notification right there!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store