Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to Use Class For Sending Notification In Android?

    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 490
    Answer it

    I wanna create a library for sending a notification to my android app but i don't know how it works and what kind of data I should give to the function in the class. this is my code for sending a notification but I don't know how to put it in the class for using in another section of the app.


    This code works in MainActivity


    Intent intent = new Intent(context, MainActivity.class);
                    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Builder b = new NotificationCompat.Builder(context);
                            .setContentTitle("Default notification")
                            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
                            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


 2 Answer(s)

  • You can use and NotificationCompact builder to create notification for your android application. If you want to show Firebase push notification you can follow this post:
    NotificationCompat.Builder builder = null;
    final int NOTIFY_ID = 0; // ID of notification
    String id = "mychannel"; // default_channel_id
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (notifManager == null) {
        notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = notifManager.getNotificationChannel(id);
        if (mChannel == null) {
            mChannel = new NotificationChannel(id, title, importance);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        builder = new NotificationCompat.Builder(context, id)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
    } else {
        builder = new NotificationCompat.Builder(context, id);
        intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                .setContentText(desciption)                            // required
                .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
                .setTicker("Accept your request")
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
    Notification notification =;
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(NOTIFY_ID, notification);
  • public class MainActivity extends ActionBarActivity {
       EditText ed1,ed2,ed3;
       protected void onCreate(Bundle savedInstanceState) {
          Button b1=(Button)findViewById(;
          b1.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                String tittle=ed1.getText().toString().trim();
                String subject=ed2.getText().toString().trim();
                String body=ed3.getText().toString().trim();
                NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify=new Notification.Builder
                   notify.flags |= Notification.FLAG_AUTO_CANCEL;
                   notif.notify(0, notify);
    Android provides NotificationManager class for this purpose. In order to use this class, you need to instantiate an object of this class by requesting the android system through getSystemService() method.

    Learn more at:
Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: