簡體   English   中英

添加Firebase子級后,在Android中獲得通知

[英]Get a Notification in Android when a Firebase child has been added

當嘗試使用偵聽器將Fire base Database子級添加到數據庫時,我試圖獲取android通知,但無法獲取通知。 我已經編寫了這個小測試應用程序的代碼,該應用程序在運行時甚至在后台都不會顯示通知。 有人可以調查一下,幫幫我嗎,我還是個初學者,一點幫助就好了!

package com.fayaz.firebasenotify;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.auth.FirebaseAuth;
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;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.view.View;


public class MainActivity extends AppCompatActivity {

private FirebaseDatabase myFirebaseRef = FirebaseDatabase.getInstance();
private DatabaseReference myRef =  myFirebaseRef.getReference();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

public void sendNotification(View view) {
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("Firebase Push Notification");
            builder.setContentText("Hello this is a test Firebase notification, a new database child has been added");
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(1, builder.build());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.i("FirebaseError", databaseError.getMessage());
        }

    };
    myRef.addValueEventListener(valueEventListener);
}
}

您應該使用ChildEventListener,

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRootRef = FirebaseDatabase.getInstance().getReference();
    builder = new NotificationCompat.Builder(this);
    mRootRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("Firebase Push Notification");
            builder.setContentText("Hello this is a test Firebase notification, a new database child has been added");
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(1, builder.build());
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

確保可以從數據庫讀取數據(檢查安全規則)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM