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
    • 42
    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);
    
                    b.setAutoCancel(true)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.drawable.ic_launcher_background)
                            .setTicker("Hearty365")
                            .setContentTitle("Default notification")
                            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
                            .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
                            .setContentIntent(contentIntent)
                            .setContentInfo("Info");
    
    
                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(1, b.build());

     

 1 Answer(s)

  • public class MainActivity extends ActionBarActivity {
       EditText ed1,ed2,ed3;
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    
          ed1=(EditText)findViewById(R.id.editText);
          ed2=(EditText)findViewById(R.id.editText2);
          ed3=(EditText)findViewById(R.id.editText3);
          Button b1=(Button)findViewById(R.id.button);
    
          b1.setOnClickListener(new View.OnClickListener() {
             @Override
             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
                   (getApplicationContext()).setContentTitle(tittle).setContentText(body).
                   setContentTitle(subject).setSmallIcon(R.drawable.abc).build();
                    
                   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: https://www.tutorialspoint.com/android/android_push_notification.htm
Sign In
                           OR                           
                           OR                           
Register

Sign up using

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