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
    • 1
    • 0
    • 0
    • 0
    • 140
    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);


 1 Answer(s)

  • 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: