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
    • 473
    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());

     

 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: https://trinitytuts.com/firebase-push-notification-android/
    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,
            PendingIntent.FLAG_ONE_SHOT);
    
    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.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notifManager.createNotificationChannel(mChannel);
        }
        builder = new NotificationCompat.Builder(context, id)
                .setContentTitle(title)
                .setContentText(desciption)
                .setContentIntent(pendingIntent)
                .setSound(defaultSoundUri)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    } 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);
        builder.setContentTitle(title)
                .setContentText(desciption)                            // required
                .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker("Accept your request")
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setPriority(Notification.PRIORITY_HIGH);
    }
    Notification notification = builder.build();
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(NOTIFY_ID, notification);
  • 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: