0

I am trying to schedule particular time and want to send notification using Firebase. I am working on the project where I have to send the notification before the match start. Currently my notification is working on real time but I can't found any way to schedule it for the specific time.

For the real time I am using this particular code.

 //Class extending service as it is a service that will run in background
public class NotificationFirebaseListener extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

String test;


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.e(TAG, "From: " + remoteMessage.getFrom());
    Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody() + "=====>");

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Message data payload: " + remoteMessage.getData());
        test = String.valueOf(remoteMessage.getData());

    }


    sendNotification(test);


    }


private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Bus Tracking")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
    }


   }
AL.
  • 36,815
  • 10
  • 142
  • 281
Shubhank Gupta
  • 833
  • 2
  • 15
  • 35

1 Answers1

3

The best way to achieve this is use the Firebase Notification composer to schedule your notifications for your app: https://firebase.google.com/docs/notifications/.

If you want another way to achieve these via code, I suggest you to schedule your notification using a ScheduledThreadPool. You can read more about this in the oracle documentation: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html.

halfer
  • 19,824
  • 17
  • 99
  • 186
Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95