我正在嘗試做一個實時消息應用程序(不像WhatsApp這樣的聊天應用程序,我有一個發佈者和很多訂閱者)。如何通過Firebase數據庫通知用戶?
我希望它發送通知的時候,我得到新的消息。
這是我的代碼(有錯誤):
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Message message = postSnapshot.getValue(Message.class);
messages.add(0, message.getTitle() + '\n' + message.getMessage(), NotificationCompat.Builder mBuilder = //cannot resolve symbol 'mBuilder'
new NotificationCompat.Builder(this) //builder (android.content.Context) in Builder cannot be applied to (anonymous com.firebase.client.ChildEventListener)
.setSmallIcon(R.drawable.logo_he_digita_homepage)
.setContentTitle(message.getTitle())
.setContentText(message.getMessage()); //')' expected
Intent resultIntent = new Intent(this, updates.class); //Cannot resolve constructor 'Intent(anonymous com.firebase.client.ChildEventListener, java.lang.Class<com.inbar.amit.ksharet>)'
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); //create (android.content.Context) in TaskStackBuilder cannot be applied to (anonymous com.firebase.client.ChildEventListener)
stackBuilder.addParentStack(updates.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(124453245, mBuilder.build()););
}
adapter.notifyDataSetChanged();
}
我使用過Android Studio。
感謝您的答案。
你的客戶端應用程序代碼,但你有一個應用程序服務器? –
@AL。我想我沒有...我在編程應用程序方面有點新。你能解釋一下我的App Server是什麼嗎? –
您正在使用Firebase,它是應用程序服務器。 –