【发布时间】: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