如何在特定时间发送Firebase通知?

编程入门 行业动态 更新时间:2024-10-20 15:54:24
本文介绍了如何在特定时间发送Firebase通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图安排特定时间,并希望使用Firebase发送通知。我正在进行项目,在比赛开始前我必须发送通知。目前我的通知正在实时工作,但我找不到任何方法来安排它的具体时间。

对于实时我使用这个特定的代码。

// 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通知撰写程序为您的应用程序安排通知: firebase.google/docs/notifications/

如果您想通过另一种方式实现这些代码,我建议您使用ScheduledThreadPool来安排您的通知。您可以在oracle文档中阅读更多关于此的信息: docs.oracle/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: firebase.google/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: docs.oracle/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

Good luck!

更多推荐

如何在特定时间发送Firebase通知?

本文发布于:2023-11-27 08:17:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   时间   如何在   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!