在我的android应用程序中使用fire base实时数据库进行聊天。我希望在我的java项目中实时添加新的子项或消息时收到通知。这可能吗?帮我举个例子
答案 0 :(得分:0)
在我为学校项目制作的应用程序中,我希望在收到新孩子时收到通知我的Firebase数据库。代码看起来像这样:
public void SetupRoastFirebase(){
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference roastRef = database.getReference(ROAST_KEY);
roastRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Roast addedRoast = dataSnapshot.getValue(Roast.class);
BroasterLogger.LogDebugMessage("New Roast added");
Gson gson = new Gson();
Intent broadcastIntent = new Intent(NEW_ROAST_READY);
broadcastIntent.putExtra(ROAST_KEY, gson.toJson(addedRoast));
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(broadcastIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
User loggedInUser = _settingsRepository.GetLoggedInUser();
if (loggedInUser == null)
return;
if (addedRoast.UserId.equals(loggedInUser.UserId))
{
BroasterLogger.LogDebugMessage("Logged in user was roasted! Posting notification!");
if (!addedRoast.DeliveredRoastToRecipient)
_roastsRepository.NotifyReceived(addedRoast.RoastId);
if (!addedRoast.DeliveredRoastToRecipient)
{
// Clicking intents heavily inspired from: https://stackoverflow.com/questions/10184351/how-to-start-activity-when-user-clicks-a-notification
Intent myRoastsActivity = new Intent(getApplicationContext(), MyProfileActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, myRoastsActivity,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), getString(R.string.channel_id))
.setContentTitle(getString(R.string.notification_title))
.setContentText(getString(R.string.notification_description))
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_round);
notificationManager.notify(101, notification.build());
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Roast addedRoast = dataSnapshot.getValue(Roast.class);
BroasterLogger.LogDebugMessage("Roast changed");
HandleAddedRoast(AddedRoast);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
基本上:我们检查了烤肉的收件人是否是当前登录的用户,如果是,我们创建了一个可点击的通知并调用了通知管理器