【问题标题】:fix grammar issues修复语法问题
【发布时间】:2020-09-08 21:27:34
【问题描述】:

我正在尝试在我的 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(),但我没有收到很多手机的任何信息:android 9、7 和 8... 所以你能帮我让它工作吗?谢谢。

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    在您的 notifications 函数中,您需要添加如下内容:

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

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

    【讨论】:

    • 在 if 和 else 中添加 notification_manager.notify(1, notification_builder.build()); 后不起作用
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多