如何在特定时间发送Firebase通知? [英] How to send the Firebase notification on particular time?
问题描述
对于实时我使用这个特定的代码。
// Class扩展服务,因为它是一个后台运行的服务
public class NotificationFirebaseListener extends FirebaseMessagingService {
private static final String TAG =MyFirebaseMsgService;
字符串测试;
$ b @Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.e(TAG,From:+ remoteMessage.getFrom() );
Log.e(TAG,Notification Message Body:+ remoteMessage.getNotification()。getBody()+=====>); (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());
$ b 解决方案实现此目的的最佳方式是使用Firebase通知撰写程序为您的应用程序安排通知: https://firebase.google.com/docs/notifications/
如果您想通过另一种方式实现这些代码,我建议您使用ScheduledThreadPool来安排您的通知。您可以在oracle文档中阅读更多关于此的信息: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html
祝您好运! / p>
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());
}
}
解决方案 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
Good luck!
这篇关于如何在特定时间发送Firebase通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文