How to trigger a notification from your Android App? - Complete source code

Published: 02 January 2021
on channel: Programmer World
3,540
32

This video shows the steps to write your own notification in your Android App.

In this video it uses a button to trigger the notification but in actual or commercial App various events can be used to trigger the notification.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code and other details can be found in the below link:
https://programmerworld.co/android/ho...


However, the main Java code is copied below also for reference:

package com.programmerworld.mynotificationapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private String CHANNEL_ID = "My Notification";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void buttonSetNotification(View view){
Intent intentNotification = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builderNotificationCompat = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentIntent(pendingIntent)
.setContentTitle("My Notification")
.setContentText("My Notification text here.")
.setSmallIcon(android.R.drawable.btn_star_big_on);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "This is my first Notification", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(0,builderNotificationCompat.build());
}
}



-


On this page of the site you can watch the video online How to trigger a notification from your Android App? - Complete source code with a duration of hours minute second in good quality, which was uploaded by the user Programmer World 02 January 2021, share the link with friends and acquaintances, this video has already been watched 3,540 times on youtube and it was liked by 32 viewers. Enjoy your viewing!