Java 在Firebase节点更新时接收通知
我有两个应用程序一个是客户预订应用程序,一个是管理员接收器应用程序。它们都连接到同一个Firebase数据库。当客户预订时,我可以在我的管理应用程序中查看。但是,我怎样才能在客户应用程序中进行预订后,在管理应用程序中收到通知呢 我已经找到了这段代码,但我如何实现它,使它显示通知,即使管理员应用程序没有打开Java 在Firebase节点更新时接收通知,java,android,firebase-realtime-database,notifications,Java,Android,Firebase Realtime Database,Notifications,我有两个应用程序一个是客户预订应用程序,一个是管理员接收器应用程序。它们都连接到同一个Firebase数据库。当客户预订时,我可以在我的管理应用程序中查看。但是,我怎样才能在客户应用程序中进行预订后,在管理应用程序中收到通知呢 我已经找到了这段代码,但我如何实现它,使它显示通知,即使管理员应用程序没有打开 Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(subject)
.setContentText(object.getString("body"))
.setAutoCancel(true)
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, ToneGenerator.MAX_VOLUME);
toneG.startTone(ToneGenerator.TONE_CDMA_HIGH_L, 3000);
((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(2000);
编辑
我的Firebase树看起来像这样
{
"Bookings",
"UserID Appears here",
"User booking info appears here"}
}
“预订”节点是一个常量,并且始终存在,一旦进行预订,就会显示用户id。我能不能有一种即使在应用程序关闭时也能运行的服务来监听“UserID”节点的更新?然后启动上面的通知方法?我从未使用过通知和服务。这是和Firebase云消息传递的典型用例。事实上,这种情况非常普遍,Firebase文档中有一个例子,名为: 云功能允许您通过在Google服务器上运行(Node.js)代码片段来响应Firebase项目中的事件。由于此代码在谷歌的服务器上运行,因此即使应用程序未处于活动状态,它也将处于活动状态。然后,此代码可以使用调用其他Firebase服务,例如此处的云消息传递 Firebase允许您向安装在设备上的应用程序发送消息,即使该应用程序未被积极使用。您通常会使用问题中的代码来响应接收到的此类消息,然后在该设备上显示本地通知。然后,当用户单击通知时,您打开应用程序,并使用实时数据库API从服务器读取(其余)数据 有关这方面的更多信息,请参见:
- 许多搜索结果都是关于和的
- 许多搜索结果都是关于和的
// Minimal Fcm Service
class FcmService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage.data.isNotEmpty().let {
// data message, handle it or start a service
Intent(this, ConnectionService::class.java).also { intent -> startService(intent) }
}
remoteMessage.notification?.let {
// notification message, you can define your custom notification here or just leave it that way
}
}
}
这就是你在舱单上登记的方式
<service
android:name="com.yourpackage.appname.FcmService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
管理应用程序的代码
// Minimal Fcm Service
class FcmService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage.data.isNotEmpty().let {
// data message, handle it or start a service
Intent(this, ConnectionService::class.java).also { intent -> startService(intent) }
}
remoteMessage.notification?.let {
// notification message, you can define your custom notification here or just leave it that way
}
}
}
这就是你在舱单上登记的方式
<service
android:name="com.yourpackage.appname.FcmService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我认为在这里发出通知或启动(前台?)服务没有问题?firebase就是这样工作的,它接收数据并执行代码,即使应用程序未打开。是的,但一个应用程序发送信息,另一个接收信息。我看不出在这里发出通知或启动(前台?)服务有问题吗?这就是firebase的工作原理,它接收数据并执行代码,即使应用程序未打开。是的,但一个应用程序发送信息,另一个接收信息。我看到您将其标记为接受答案,但我仍然建议您阅读Frank建议的内容,以便您可以学习,另外,请查看我的上一个链接,了解正确的fcm服务应该是什么样子。我看到您将其标记为已接受答案,但我仍然建议您阅读Frank建议的内容,以便您能够了解。另外,请查看我的上一个链接,了解正确的fcm服务应该是什么样子