解决语法问题



我正试图在我的android应用程序中发出通知,当我收到一些号码时,我从服务器Spring Boot收到了我的数据,我想通知我的应用程序,所以这是我的代码:

public class InformationNow extends AppCompatActivity implements 
SwipeRefreshLayout.OnRefreshListener {
private  InformationNowAdapter informationNowAdapter ;
private String url = "http://192.168.1.90:9090/TrashCanData/lastrow";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_now);


refresh = (SwipeRefreshLayout) findViewById(R.id.Swiper);
recyclerView = (RecyclerView) findViewById(R.id.Now);

dialog = new Dialog(this);

Timer repeatTask = new Timer();
repeatTask.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
trashCanModels.clear();
getData();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 0, 1000);


}

private void getData() throws InterruptedException  {
refresh.setRefreshing(false);

arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;


try {
//JSONArray jsonArray = response.getJSONArray("users");
jsonObject = response.getJSONObject(0);
final TrashCanModel  cat = new TrashCanModel();
cat.setDistance(jsonObject.getInt("distance"));
cat.setTemperature(jsonObject.getInt("temperature"));
cat.setHumidity(jsonObject.getInt("humidity"));
Log.e("*********", cat.getDistance().toString());
Log.e("*********", cat.getTemperature().toString());
Log.e("*********", cat.getHumidity().toString());

Thread t = new Thread(new Runnable() {
public void run() {
try{
if(cat.getDistance()<8 || cat.getHumidity()>90 || cat.getTemperature()>27){

notifications();

}
}catch (Exception e){
Log.e("EXCEPTION", "Inside thread: " + e.getMessage());
}
}
});
t.start();



trashCanModels.add(cat);


} catch (JSONException e) {
e.printStackTrace();

}

adapterPush(trashCanModels);
refresh.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

requestQueue = Volley.newRequestQueue(InformationNow.this);
requestQueue.add(arrayRequest);

//Thread.sleep(9000);

}

private void notifications() {

NotificationCompat.Builder notification_builder;
NotificationManager notification_manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String chanel_id = "3000";
CharSequence name = "Channel Name";
String description = "Chanel Description";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.BLUE);
notification_manager.createNotificationChannel(mChannel);
notification_builder = new NotificationCompat.Builder(this, chanel_id);
} else {
notification_builder = new NotificationCompat.Builder(this);
}
notification_builder.setSmallIcon(R.drawable.logo1)
.setContentTitle("Notification Title")
.setContentText("Notification Body")
.setAutoCancel(true);
}

private void adapterPush(ArrayList<TrashCanModel> trashCanModels) {

informationNowAdapter = new InformationNowAdapter(this, trashCanModels);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(informationNowAdapter);


}


@Override
public void onRefresh() {
trashCanModels.clear();
try {
getData();
//Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}


}


}

但我没有收到任何通知,在我的情况下,该功能名为notifications(),但我没有从很多手机收到任何消息:安卓9、7和8。。。所以你能帮我让它发挥作用吗。谢谢

notifications函数中,您需要添加以下内容:

// notificationId is a unique int for each notification that you must define
notification_manager.notify(notificationId, notification_builder.build());

cf文档:https://developer.android.com/training/notify-user/build-notification