首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当Firebase节点更新时接收通知

当Firebase节点更新时接收通知
EN

Stack Overflow用户
提问于 2020-01-26 15:50:13
回答 2查看 330关注 0票数 0

我有两个应用程序,一个客户预订应用程序和一个管理接收应用程序。它们都连接到同一个Firebase数据库。当客户进行预订时,我可以在我的管理应用程序中查看。但是,一旦在客户应用程序中进行了预订,如何才能在管理应用程序中接收通知呢?

我已经找到了这段代码,但我如何实现它,以便它显示通知,即使管理应用程序没有打开?

代码语言:javascript
运行
复制
 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树看起来像这样

代码语言:javascript
运行
复制
{
"Bookings",
"UserID Appears here",
"User booking info appears here"}
}

预订节点是一个常量,并且总是在那里,一旦预订完成,用户id就会出现。我是否可以在应用程序关闭时运行某种服务,监听"UserID“节点的更新?然后启动上面的通知方法?我从来没有处理过通知和服务。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-26 16:52:58

管理应用程序的代码

代码语言:javascript
运行
复制
// 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
        }
    }
}

这就是你如何在你的舱单上注册的。

代码语言:javascript
运行
复制
<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代码

代码语言:javascript
运行
复制
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
        }
    }
}

链接到java中的正式Fcm服务

票数 1
EN

Stack Overflow用户

发布于 2020-01-26 16:40:19

这是云函数和Firebase消息传递的典型用例。事实上,这个场景非常常见,在Firebase文档中有一个例子叫做:当有趣的事情发生时通知用户

云功能允许您通过在Google服务器上运行(Node.js)代码来响应Firebase项目中的事件。因为这段代码运行在Google的服务器上,所以即使应用程序不活动,它也是活动的。然后,该代码可以使用Firebase Admin调用其他Firebase服务,例如此处的云消息传递。

Firebase 云消息传递允许您将消息发送到安装在设备上的应用程序,即使该应用程序未被有效使用。您通常会在您的问题中使用代码来响应这样的消息,然后在该设备上显示一个本地通知。然后,当用户单击通知时,打开应用程序,并使用Realtime从服务器读取(其余的)数据

有关此问题的更多信息,请参见:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59919988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档