我正在使用Firebase实时数据库开发聊天应用。我能够正确地发送和接收消息。现在,我想在收到新消息时实现通知。即,每当有任何用户发送消息时,推送通知就会触发其他用户设备,就像whatsapp一样。 我主要担心的是如何在我给出的下面代码中做到这一点?
OnlineActivity.java
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.text.format.DateFormat;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import hani.momanii.supernova_emoji_library.Actions.EmojIconActions;
import hani.momanii.supernova_emoji_library.Helper.EmojiconEditText;
import hani.momanii.supernova_emoji_library.Helper.EmojiconTextView;
import static com.yef.youthempower.youth.R.layout.activity_online;
public class OnlineActivity extends AppCompatActivity {
private static int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_online;
//Add Emojicon
EmojiconEditText emojiconEditText;
ImageView emojiButton,submitButton;
EmojIconActions emojIconActions;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SIGN_IN_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
Snackbar.make(activity_online, "Successfully signed
in.Welcome!", Snackbar.LENGTH_SHORT).show();
displayChatMessage();
}
else{
Snackbar.make(activity_online,"We couldn't sign you in.Please
try again later", Snackbar.LENGTH_SHORT).show();
finish();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_online);
activity_online=(RelativeLayout)findViewById(R.id.activity_online);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
emojiButton = (ImageView)findViewById(R.id.emoji_button);
submitButton = (ImageView)findViewById(R.id.submit_button);
emojiconEditText =
(EmojiconEditText)findViewById(R.id.emojicon_edit_text);
emojIconActions = new
EmojIconActions(getApplicationContext(),
activity_online,emojiButton,emojiconEditText);
emojIconActions.ShowEmojicon();
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(emojiconEditText.getText().toString(),
FirebaseAuth.getInstance().getCurrentUser().getEmail()));
emojiconEditText.setText("");
emojiconEditText.requestFocus();
}
});
//Check if not sign-in then navigate Signin page
if(FirebaseAuth.getInstance().getCurrentUser() == null)
{
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(),SIGN_IN_REQUEST_CODE);
}
else
{
Snackbar.make(activity_online,"Welcome "+FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
//Load content
displayChatMessage();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
private void displayChatMessage() {
ListView listOfMessage = (ListView)findViewById(R.id.list_of_message);
adapter = new FirebaseListAdapter<ChatMessage>(this,ChatMessage.class,R.layout.list_items,FirebaseDatabase.getInstance().getReference())
{
@Override
protected void populateView(View v, ChatMessage model, int position) {
//Get references to the views of list_items.xml
TextView messageText, messageUser, messageTime;
messageText = (EmojiconTextView) v.findViewById(R.id.message_text);
messageUser = (TextView) v.findViewById(R.id.message_user);
messageTime = (TextView) v.findViewById(R.id.message_time);
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
}
};
listOfMessage.setAdapter(adapter);
}
}
答案 0 :(得分:0)
您可以轻松地从firebase控制台发送通知, 请参阅文档:https://firebase.google.com/docs/cloud-messaging/
https://proandroiddev.com/mastering-firebase-notifications-36a3ffe57c41
但是你要求的是一种自动通知,这有点棘手,但你可以通过
来做到这一点使用firebase云功能。
视频教程:https://www.youtube.com/playlist?list=PLGCjwl1RrtcRHjHyZAxm_Mq4qvtnundo0
通过对您的firebase服务器的发布请求,
文档:https://firebase.google.com/docs/cloud-messaging/send-message
How to send device to device messages using Firebase Cloud Messaging?