我有两个应用程序,一个客户预订应用程序和一个管理接收应用程序。它们都连接到同一个Firebase数据库。当客户进行预订时,我可以在我的管理应用程序中查看。但是,一旦在客户应用程序中进行了预订,如何才能在管理应用程序中接收通知呢?
我已经找到了这段代码,但我如何实现它,以便它显示通知,即使管理应用程序没有打开?
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“节点的更新?然后启动上面的通知方法?我从来没有处理过通知和服务。
发布于 2020-01-26 16:52:58
管理应用程序的代码
// 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>
编辑:
通知消息: FCM代表客户端应用程序自动向终端用户设备显示消息。通知消息具有一组预定义的用户可见键和自定义键值对的可选数据有效负载。 数据消息: 客户端应用程序负责处理数据消息。数据消息只有自定义的键值对。
Edit2: Java代码
public class FcmService extends FcmService {
@Override
public void onMessageReceived(@NotNull RemoteMessage remoteMessage) {
if (!remoteMessage.getData().isEmpty()){
// data message
// start a service or handle it the way you want
}
if (remoteMessage.getNotification() != null){
// notification message
}
}
}
发布于 2020-01-26 16:40:19
这是云函数和Firebase消息传递的典型用例。事实上,这个场景非常常见,在Firebase文档中有一个例子叫做:当有趣的事情发生时通知用户
云功能允许您通过在Google服务器上运行(Node.js)代码来响应Firebase项目中的事件。因为这段代码运行在Google的服务器上,所以即使应用程序不活动,它也是活动的。然后,该代码可以使用Firebase Admin调用其他Firebase服务,例如此处的云消息传递。
Firebase 云消息传递允许您将消息发送到安装在设备上的应用程序,即使该应用程序未被有效使用。您通常会在您的问题中使用代码来响应这样的消息,然后在该设备上显示一个本地通知。然后,当用户单击通知时,打开应用程序,并使用Realtime从服务器读取(其余的)数据
有关此问题的更多信息,请参见:
https://stackoverflow.com/questions/59919988
复制相似问题