有没有什么办法可以自动发送推送通知时,我添加新闻到我的火力
这是用来添加新闻火基地有没有什么办法可以自动发送推送通知时,我添加新闻到我的火力
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);
}
});
}
}
,这是所使用的文件中的java文件显示信息列表视图
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();
}
});
}
}
现在我知道,你可以从通知栏是火基地页面上的消防基地发送的信息,但我不想因为手动添加的信息我在我的应用程序中的这些页面是为了消息,我不能总是手动执行b因为我不是唯一一个添加这些消息的人,所以我需要应用程序来显示一个简单的酒吧,在消息声音的背景上显示新消息和我的应用程序名称和徽标。任何人都可以帮忙吗?
// 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();
}
});
谢谢你的帮助,但你能指导我多一点,因为我是使用Firebase作为我的服务器。我如何将这段代码添加到消防基地?以及如何注册我的所有设备ID到网络服务器?在我的情况下,应用程序在应用程序商店,并有很多用户使用不同的设备。 – Ali
public static void sendNotificationToUser(String user,final String message){ Firebase ref = new Firebase(FIREBASE_URL); 最终Firebase通知= ref.child(“notificationRequests”); Map notification = new HashMap (); notification.put(“username”,user); notification.put(“message”,message); notifications.push()。的setValue(通知); } – param
我已经编辑了答案,它会做你的工作,不要忘记vote.happy编码。 – param
保存列表计数共享偏好。然后检查您的清单数量是否超过上次保存的清单数量。如果它更大,那么建立一个本地通知&显示你的列表中最后一个添加的项目作为通知。 每次更改Firebase上的数据时,都不需要从Firebase推送通知。
每次添加新闻时,都可以在数据库中创建一个名为“notifications”的子项,并且您可以为要发布的特定新闻项添加标题和要生成的通知消息。
确保所有使用您的应用程序的用户都订阅了某个主题。
最后,运行一个服务器端脚本(我使用Node.js来实现此目的),它监听“notifications”子节点,并且每次向其添加子节点时,都会获取数据快照,检索标题和消息,并向您的用户订阅的主题发送主题通知。
只要您的应用程序中有一个活动的服务并且它在后台运行,它就会自动收到推送通知。
有关主题通知和用于服务器通信的REST API的更多信息,请转至here。
当他/她发布新闻时,您可以直接从用户设备发送通知。 –
是的,我想过这种方式,我使用OnButtonClick添加消息按钮的消息得到发送,但问题我不知道该怎么做。 – Ali
http://*.com/questions/38089148/send-push-notification-from-server-to-android-device-in-java –