doushouxie7064 2017-02-04 05:54
浏览 135

使用json / php的Android Studio简单通知

I have built a simple android app for internal use only. This app, sends data (GPS coordinates) every few seconds which saves to mysql via a simple PHP script. I do need the users Android device to display a preset notification (which will be displayed if the phone is in standby more, viewing another app etc..) if there is a certain response from json.

Most of the tutorials i have seen, advise that this is done using GCM. I would have thought, a 'self pushed' notification should be easier than this.

e.g if json replies with "notify: 1" then androids notification will state 'There is a job waiting'.

Is there an easy way to do this? if so, please guide me to an easy resolution.

new HttpRequestTask(
                new HttpRequest("https://www.autoflora.net/driver/gps.php?Driver_ID=" + driver_id + "&latlong=" +
                        location.getLatitude() + "*" + location.getLongitude(), HttpRequest.POST, "{ \"some\": \"data\" }"),
                new HttpRequest.Handler() {
                    @Override
                    public void response(HttpResponse response) {
                        if (response.code == 200) {
                            Log.d(this.getClass().toString(), "Request successful!");
                        } else {
                            Log.e(this.getClass().toString(), "Request unsuccessful: " + response);
                        }
                    }
                }).execute();
  • 写回答

1条回答 默认 最新

  • dtudj42064 2017-02-04 07:47
    关注

    Once you get the response body from httpresponse. Create JSON object from that, for example

    ...
    JSON response = new JSON(responseBody); //responseBody is what you received from server
    
    if(response.has("notify") && (response.getInt("notify") == 1))
    {
      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
      notificationBuilder.setContentTitle(title); //Set notification title
      notificationBuilder.setContentText(message); // set notification text
    
      NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
      notificationManager.notify(0 /* ID of notification */, 
      notificationBuilder.build());
    
    }
    ...
    

    Hope this helps

    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝