当 firebase 数据库发生更改而不使用控制台或第 3 方服务器时,是否可以使用 FCM 发送推送通知?

Is it possible to send push notifications using FCM when a change occurs in firebase database without using the console or a 3rd party server?

我正在编写一个 android 应用程序,它使用 firebase 数据库作为后端。当数据库中的字段更新时,我需要推送通知特定设备。我一直在搜索并找到使用 php、其他服务器或 firebase 控制台执行此操作的示例。我真的需要再买一台服务器吗?

更新于 2017-07-02

自 2017 年 3 月起,Firebase 提供 Cloud Functions for Firebase, which allows you to run JavaScript functions on Google's servers in response to events in Firebase (such as a database update). The first sample use-case is Notify users (using Cloud Messaging) when something interesting happens,因此我建议阅读更多相关信息。

您不需要发送推送通知。因为 Firebase 提供了 Firebase 数据库的实时同步。当您在客户端的数据库中添加一个侦听器时,它会在每次更改时被调用并且可以完全控制它。 这是注册更改侦听器的示例:

// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");

myRef.setValue("Hello, World!");

// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this location is updated.
        String value = dataSnapshot.getValue(String.class);
        Log.d(TAG, "Value is: " + value);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
        Log.w(TAG, "Failed to read value.", error.toException());
    }
});