The Startup
Published in

The Startup

Android Notifications | Notification Channel | Big Picture Style | Android Studio

Android Notifications | Notification channel | Big Picture Style | Android Studio

In this tutorial, we’re going to see how to send text and image notifications on android. Before going to create it, first see what notification is?

What is Android Notification?

A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.

For more Click Here.

Now let’s see how to create it.

Step 1: Design Main XML

In the main XML file create a hook from where we will call our notification channel to show the notification. Here I use a Button to invoke a notification channel.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonShowNotification"
android:text="@string/show_notification"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Step 2: Add Functionality

  • Set the OnClickListener on the button that we created to invoke the notification channel.
findViewById(R.id.buttonShowNotification).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showNotification(); // for notification with Text
}
});
  • now create showNotification() function in the MainActivity.java class. This function is for notification with text.
//text Notification
private void showNotification(Bitmap bitmap) {

}
  • create a notification channel and set the notification title, text etc.
//text Notification
private void showNotification(Bitmap bitmap) {
int noificationId = new Random().nextInt(100);
String channelId = "notification_channel_3";
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext(), channelId
);
builder.setSmallIcon(R.drawable.ic_action_name);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentTitle("Notification 3"); // make suer change the channel for image
builder.setContentText("Lorem Ipsum is simply dummy text of the printing and typesetting industry");
//notification for image
builder.setStyle(new NotificationCompat.BigPictureStyle().
bigPicture(bitmap));
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if (notificationManager != null && notificationManager.
getNotificationChannel(channelId) == null){
NotificationChannel notificationChannel = new NotificationChannel(
channelId, "Notification channel 1",
NotificationManager.IMPORTANCE_HIGH
);
notificationChannel.setDescription("This notification channel is used to notify user");
notificationChannel.enableVibration(true);
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
}
Notification notification = builder.build();
if (notificationManager != null){
notificationManager.notify(noificationId, notification);
}
}
  • now create a function for notification with image, here I named it as showNotificationWithImage().
//notification with image
@SuppressLint("StaticFieldLeak")
private void showNotificationWithImage(){

}
  • add the functionality for the image notification, also don’t forget to initialize the bitmap in the showNotification() function, because this the main function which creates the notification for us.
//notification with image
@SuppressLint("StaticFieldLeak")
private void showNotificationWithImage(){
new AsyncTask<String, Void, Bitmap>(){
@Override
protected Bitmap doInBackground(String... strings) {
InputStream inputStream;
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
inputStream = connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
}catch (Exception ignored){ }
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
showNotification(bitmap);
}
}.execute("paste-your-image-link-here");
}

Output (Notification with text)

Output (Notification with Image)

Want to watch how to create it, visit my YouTube channel and watch it:

YouTube: Click Here

You can also follow me on Instagram and Facebook

Instagram: Android App Developer
Facebook: GBAndroidBlogs

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Android’s Media Scanner: How do I remove files?

Firebase Phone number authentication in Android | Android Studio | Java

Migrating From RxKotlin to Kotlin Coroutines

repeatOnLifecycle API design story

HMS Accountkit :There are up to one billion HUAWEI ID users across the globe.

Android Animation | Day and Night Mode | Dark and Night Switch

Android 102 : Why does dagger generate MembersInjector for us?

Android Stack Weekly — Issue #15

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
Golap Gunjan Barman

Golap Gunjan Barman

Hi everyone, myself Golap an Android app developer with UI/UX designer.

More from Medium

Exploring Photo Picker in Android 13

Monitoring Network Bandwidth Usage in the Doubtnut app.

Find the introduction Sliders and Huawei Account Kit Integration in Money Management Android app…

Meet DataStore — The New SharedPreferences for Android