gpt4 book ai didi

java - 用于聊天应用的 Firebase 推送通知

转载 作者:行者123 更新时间:2023-11-29 02:28:58 32 4
gpt4 key购买 nike

我正在使用 Firebase 实时数据库开发聊天应用。我已经能够正常发送和接收消息。现在,我想在收到新消息时实现通知。即每当任何用户发送消息时,推送通知都会在其他拥有该应用程序的用户设备上触发,就像 whatsapp 一样。我主要关心的是如何在下面给出的代码中执行该部分?

在线 Activity .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);
}

最佳答案

您可以轻松地从 firebase 控制台发送通知,请参阅文档:https://firebase.google.com/docs/cloud-messaging/

https://proandroiddev.com/mastering-firebase-notifications-36a3ffe57c41

但是你要求某种自动通知,这有点棘手,但你可以通过

  1. 使用 Firebase 云函数。

    视频教程:https://www.youtube.com/playlist?list=PLGCjwl1RrtcRHjHyZAxm_Mq4qvtnundo0

  2. 通过向您的 firebase 服务器发送请求,

文档:https://firebase.google.com/docs/cloud-messaging/send-message

How to send device to device messages using Firebase Cloud Messaging?

关于java - 用于聊天应用的 Firebase 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50687433/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com