【问题标题】:Android Studio simple notification using json/php使用 json/php 的 Android Studio 简单通知
【发布时间】:2017-02-04 05:54:04
【问题描述】:

我已经构建了一个仅供内部使用的简单 Android 应用程序。这个应用程序每隔几秒发送一次数据(GPS 坐标),通过一个简单的 PHP 脚本保存到 mysql。如果json有一定的响应,我确实需要用户的Android设备显示一个预设的通知(如果手机处于待机状态,查看另一个应用程序等时会显示)。

我看过的大多数教程都建议使用 GCM 完成。我原以为,“自推送”通知应该比这更容易。

例如 如果 json 回复“通知:1”,那么 androids 通知将声明“有工作等待”。

有没有简单的方法来做到这一点?如果是这样,请指导我一个简单的解决方案。

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();

【问题讨论】:

    标签: php json android-studio push-notification notifications


    【解决方案1】:

    一旦您从 httpresponse 获得响应正文。 从中创建 JSON 对象, 例如

    ...
    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());
    
    }
    ...
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多