1
我試圖在使用偵聽器將Fire base數據庫子項添加到數據庫時嘗試獲取android通知,但無法獲取通知。我編寫了這個小測試應用程序,它不會在應用程序運行時顯示通知,甚至在後臺顯示。有人可以看看這個,並幫助我,我仍然是一個初學者,一點幫助將是美好的!添加Firebase子項時在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);
}
}
嘿T.S,我試圖添加onChildAdded(),它似乎不工作!你可以請我PM我[email protected],只是爲了確保,我不需要一個監聽器,以便在應用程序未運行時獲得通知。 –
嘿T.S,我知道它的工作。非常感謝你的幫助! –
如果我收到了一條新消息,我應該如何在應用程序處於後臺/死亡狀態時生成通知? – user512