这是用于向fire base添加新闻的java文件
public class newsadding extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("allNews");
EditText date, name;
Button btsave, btdelete, btshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsadding);
date =(EditText) findViewById(R.id.id);
name =(EditText) findViewById(R.id.editName);
btsave = (Button) findViewById(R.id.btSave);
btdelete = (Button) findViewById(R.id.btDelete);
btshow = (Button) findViewById(R.id.btShow);
// save the record
btsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n = date.getText().toString();
String nom = name.getText().toString();
try {
myRef.child("allNews").push().setValue(n+" : "+nom);
Toast.makeText(newsadding.this, "Record saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(newsadding.this, "Record not saved" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
// delete a record
btdelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String n = date.getText().toString();
String q = database.getReference().toString();
Toast.makeText(newsadding.this, "record deleted", Toast.LENGTH_SHORT).show();
}
catch (Exception ex) {
Toast.makeText(newsadding.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
// show all records
btshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i1 = new Intent(newsadding.this, usernews.class);
startActivity(i1);
}
});
}
}
这是用于在列表视图中显示信息的文件
public class usernews extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myRef = database.getReference("allNews").child("allNews");
TextView fullnews;
ProgressBar loading;
private ListView lsStudents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_usernews);
loading = (ProgressBar) findViewById(R.id.loading);
lsStudents=(ListView) findViewById(R.id.list);
fullnews=(TextView) findViewById(R.id.fullnews);
fullnews.setVisibility(View.INVISIBLE);
Toast.makeText(usernews.this,
"يرجى التأكد من أنك متصل بالإنترنت إذالم تكن متصل بعد...", Toast.LENGTH_LONG).show();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
final List<String> areas = new ArrayList<String>();
for (DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
// Get value from areaSnapShot not from dataSnapshot
String value1 = String.valueOf(areaSnapshot.getValue());
areas.add(value1);
if (lsStudents.equals(null)){
loading.setVisibility(View.VISIBLE);
}
if (!lsStudents.equals(null)){
loading.setVisibility(View.GONE);
}
}
String value2 = String.valueOf(dataSnapshot.getValue());
ArrayList<String> areas2 = new ArrayList<String>(areas);
Collections.reverse(areas2);
ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(usernews.this,android.R.layout.simple_expandable_list_item_1, areas);
lsStudents.setAdapter(areasAdapter);
lsStudents.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
fullnews.setVisibility(View.VISIBLE);
String newss = (lsStudents.getItemAtPosition(position).toString());
fullnews.setText(newss);
}
});
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Toast.makeText(usernews.this,
error + "!!!!!خطاء في الاتصال !!!!!!", Toast.LENGTH_LONG).show();
}
});
}
}
现在我知道您可以从火灾基页上的通知栏发送火灾基地的信息,但我不想手动添加信息,因为我的应用程序中的这些页面用于新闻和我不能总是手动操作,因为我并不是唯一一个添加这些新闻的人,所以我需要应用程序显示一个简单的栏,在背景上显示新消息和我的应用程序名称和徽标,并带有消息声音。任何人都可以帮忙吗?
答案 0 :(得分:1)
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "API_KEY_HERE";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification( ) throws Exception {
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + authKey);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject data = new JSONObject();
data.put("to","/topics/foo-bar");
JSONObject info = new JSONObject();
info.put("title", "FCM Notificatoin Title"); // Notification title
info.put("body", "Hello First Test notification"); // Notification body
data.put("data", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
btsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pushFCMNotification();
}
});
答案 1 :(得分:1)
每次添加一些新闻时,您都可以在数据库中创建名为“notifications”的子项,并且可以为您发布的特定新闻项添加要生成的通知的标题和消息。
确保使用您应用的所有用户都订阅了某个主题。
最后,运行一个服务器端脚本(我为此目的使用Node.js)来监听“notifications”子节点,每次添加一个子节点时,都需要一个数据快照,检索标题和消息,并向用户订阅的主题发送主题通知。
只要您的应用中有活动服务并且它在后台运行,它就会自动收到推送通知。
有关主题通知和用于服务器通信的REST API的更多信息,请转到here。
答案 2 :(得分:0)
首先,您必须在您的客户端应用程序中注册和订阅主题。一旦订阅,他们将收到通知。要发送通知,请参阅this firebase教程
答案 3 :(得分:0)
将您的列表计数保存在共享首选项中。然后检查您的列表计数是否超过上次保存的列表计数。如果它更大,那么建立一个本地通知&amp;将您上次添加的列表项目显示为通知。 每次更改Firebase上的数据时都无需从Firebase推送通知。