【问题标题】:how do i get my android app which is connected to a realtime firebase database to create a toast on update in the realtime database?如何让我的 android 应用程序连接到实时 firebase 数据库以在实时数据库中创建更新 toast?
【发布时间】:2021-03-05 04:00:39
【问题描述】:

这是我的 fireabase 数据库 View Database

我想要的只是在我的 android 应用程序中生成一个 toast 或一个通知,说明在 faces 或 movement 目录下添加了哪些更改或哪些新条目。我正在尝试使用 addValueEventListener() 方法,但我无法让它工作。下面是我的 android 应用程序中的 onCreate() 方法。我试图通过遵循这个https://www.youtube.com/watch?v=vxCDS6DaFSQ youtube 教程来实现这一点。我已经将我的应用程序连接到了 Firebase。另外,我正在尝试通过不使用 firebase 云功能来完成此操作。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(this, "Firebase Successful", Toast.LENGTH_SHORT).show();

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");
    myRef.child("App")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    Iterable<DataSnapshot> children = snapshot.getChildren();

                    for (DataSnapshot child: children)
                    {
                        String value = child.getValue(String.class);
                        Toast.makeText(MainActivity.this, "Data" + value, Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
}

如果这不能通过使用 addValueEventListener() 方法来实现,还有其他相对简单的替代方法吗?

这是我的 MainActivity.java(有两个按钮)

package com.example.notification_test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
    private final String CHANNEL_ID = "personal_notifications";
    private final int NOTIFICATION_ID = 001;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "Firebase Successful", Toast.LENGTH_SHORT).show();

        DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
        myRef.child("App").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Iterable<DataSnapshot> children = snapshot.getChildren();

                for (DataSnapshot child: children)
                {
                    String value = child.getValue(String.class);
                    Toast.makeText(MainActivity.this, "Data" + value, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    public void testFirebase(View view)
    {
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");
        myRef.setValue("Hello");
    }

    public void displayNotification(View view)
    {
        createNotificationChannel();
        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        builder.setSmallIcon(R.drawable.ic_sms);
        builder.setContentTitle("Simple Notification");
        builder.setContentText("This is a simple notification..");
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());

    }

    private void createNotificationChannel()
    {
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
        {
            CharSequence name = "Personal Notifications";
            String description = "Include all the personal notifications";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);

            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

        }

    }

}```

【问题讨论】:

  • 也许你可以使用推送通知。在这种方法中,用户不必打开应用程序。您向所有用户发送了消息。我认为这种方法对你更有用。
  • 是的,但我必须为此使用 firebase 函数,而我以前从未使用过它们。

标签: android firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

我假设您的问题在这一行

 DatabaseReference myRef = database.getReference("message");

根据Firebase Docs 您不应该将 "message" 传递给数据库引用。

【讨论】:

  • 应用程序一打开就一直崩溃,即使我没有将任何字符串传递给 getReference()。
  • 什么是 Logcat 输出?
  • 我最好的猜测是您将 HashMap 而不是 String 传递给 firebase。检查您的mainActivity 的第 40 行。
【解决方案2】:

这应该适合你:

 DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
 myRef.child("App").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Iterable<DataSnapshot> children = snapshot.getChildren();

                for (DataSnapshot child: children)
                {
                    String value = child.getValue(String.class);
                    Toast.makeText(MainActivity.this, "Data" + value, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
}

【讨论】:

  • 当我这样做时,应用程序一启动就崩溃了。您认为问题出在我对 child.getValue() 方法的实现上吗?因为我使用的是 String.class。
  • 我意识到问题出在哪里,只是编辑了我的答案。现在就试试。它应该可以正常工作。
  • 每当我启动应用程序时,它仍然会一直停止/崩溃。我还尝试在没有此 addVlaueEventLsitener() 函数部分的情况下运行该应用程序,以查看该应用程序是否还有其他问题。但是没有这部分,其他一切都运行良好。
  • 请分享崩溃报告。这样就帮不上忙了
  • 没有显示崩溃报告。它只显示主页并在一秒钟内关闭应用程序。这是我几次启动应用程序时设备的屏幕截图imgur.com/obB05rV
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 2018-06-15
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多