使用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个回答

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问