Menus

Showing posts with label Create and Display Notification on Android with Example. Show all posts
Showing posts with label Create and Display Notification on Android with Example. Show all posts

Monday 4 May 2020

How to add custom notification in android app


Create and Display Notification on Android with Example






import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.View; import android.widget.Button; public class NotficationtestActivity extends AppCompatActivity { NotificationCompat.Builder notification; private static final int appuniqueID = 10012; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notficationtest); notification = new NotificationCompat.Builder(this); notification.setAutoCancel(true); Button btnNotification = (Button) findViewById(R.id.btnNotificationttest); btnNotification.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Build the notification notification.setSmallIcon(R.drawable.photo); notification.setTicker("Ticker"); notification.setWhen(System.currentTimeMillis()); notification.setContentTitle("TechProgrammingIdeas"); notification.setContentText("Added new video. Subscribe my channel"); Intent intent = new Intent(NotficationtestActivity.this,NotficationtestActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(NotficationtestActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); notification.setContentIntent(pendingIntent); //Builds notification and issue it NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(appuniqueID,notification.build()); } }); } }