Ver código fonte

1.修改Android Apex Drivers推送处理。

Pen Li 7 anos atrás
pai
commit
b54c2294b0

+ 118 - 10
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -23,6 +23,8 @@ import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
 import android.util.Log;
 
+import com.usai.redant.apexdrivers.detail.DetailActivity;
+import com.usai.redant.apexdrivers.home.HomeFragment;
 import com.usai.redant.apexdrivers.network.Network;
 import com.usai.redant.apexdrivers.receiver.ApexDriverAlarmReceiver;
 import com.usai.redant.apexdrivers.utils.OperationQueue;
@@ -33,6 +35,8 @@ import org.json.JSONObject;
 
 import java.lang.reflect.Field;
 
+import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
+
 public class ApexDriverApplication extends Application {
 
     public final static String secretKey = "usai";
@@ -543,8 +547,12 @@ public class ApexDriverApplication extends Application {
             break;
             case BackgroundReportTypeAllow: {
 
-                String location = null;
-                reportLocationForOrder(location,orderId);
+                Location location = getCurrentLocation();
+                String latlon = "-999,-999";
+                if (location != null) {
+                    latlon = location.getLatitude() + "," + location.getLongitude();
+                }
+                reportLocationForOrder(latlon,orderId);
             }
             break;
         }
@@ -552,13 +560,107 @@ public class ApexDriverApplication extends Application {
 
     public void receiveNotification(JSONObject notification) {
 
+        if (notification != null) {
+
+            JSONObject aps = notification.optJSONObject("aps");
+            if (aps != null) {
+
+                int report_location = aps.optInt("report-location",0);
+                final String orderID = aps.optString("order-id");
+
+                if (report_location != 0) {
+
+                    reportLocationForOrder(orderID);
+
+                } else {
+
+                    int is_order = aps.optInt("is-order",0);
+                    if (is_order != 0) {
+
+                        sendBroadcast(new Intent(HomeFragment.HomeReloadBroadcastAction));
+
+                        boolean isActive = true; // 程序是否在前台
+                        if (isActive) {
+                            // 弹窗提示
+
+                            final int orderType = aps.optInt("order-type");
+                            final String orderType2 = aps.optString("order-type2");
+                            final String statusNo = aps.optString("status-no");
+                            int required = aps.optInt("required");
+                            String msg = aps.optString("message");
+                            if (TextUtils.isEmpty(msg)) {
+                                msg = orderID + " status changed,view detail?";
+                            }
+
+                            AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
+                            builder.setTitle("Message");
+                            builder.setMessage(msg);
+                            if (required == 0) {
+                                builder.setNegativeButton("Cancel",null);
+                            }
+                            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
+                                @Override
+                                public void onClick(DialogInterface dialog, int which) {
+                                    showDetail(orderID,orderType,orderType2,statusNo);
+                                }
+                            });
+                            builder.show();
+
+                        } else {
+                            // 发通知
+                            popLocalNotification(notification);
+                        }
+
+                    }
+
+                }
+
+            }
+
+        }
+
     }
 
-    public void popLocalNotification(String title, String msg) {
+    public void popLocalNotification(JSONObject notification) {
+
+        if (notification == null) {
+            return;
+        }
+        JSONObject aps = notification.optJSONObject("aps");
+        if (aps == null) {
+            return;
+        }
+
+        int id = aps.optInt("id");
+        JSONObject alert = aps.optJSONObject("alert");
+        String title = null,msg = null;
+        if (alert != null) {
 
+            title = alert.optString("title");
+            msg = alert.optString("body");
+        }
 
-        Intent intent = new Intent();
-        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
+        if (TextUtils.isEmpty(title) || TextUtils.isEmpty(msg)) {
+            return;
+        }
+
+//        final String orderID = aps.optString("order-id");
+//        final int orderType = aps.optInt("order-type");
+//        final String orderType2 = aps.optString("order-type2");
+//        final String statusNo = aps.optString("status-no");
+//
+//        Intent intent = DetailActivity.build(getApplicationContext(),orderID,orderType,orderType2,statusNo);
+//        intent.putExtra("goHome",true);
+        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
+        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        intent.putExtra("aps",aps.toString());
+
+        /**
+         *
+         * requestCode: 需要保证不同,否则id不通的intent取到的extra也是同一个
+         * */
+        int requestCode = id;
+        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), requestCode, intent,FLAG_UPDATE_CURRENT);
 
         //1.获取系统通知的管理者
         NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@@ -571,22 +673,28 @@ public class ApexDriverApplication extends Application {
             nm.createNotificationChannel(channel);
 
             noti = new NotificationCompat.Builder(this, CHANNEL_ID)
-                    .setContentTitle("我是大的标题")
-                    .setContentText("我是内容")
+                    .setContentTitle(title)
+                    .setContentText(msg)
                     .setSmallIcon(R.drawable.icon_50)
                     .setContentIntent(contentIntent)
                     .build();
         } else {
 
             noti = new Notification.Builder(this)
-                    .setContentTitle("我是大的标题")
-                    .setContentText("我是内容")
+                    .setContentTitle(title)
+                    .setContentText(msg)
                     .setSmallIcon(R.drawable.icon_50)
                     .setContentIntent(contentIntent)
                     .build();
 
         }
 
-        nm.notify(1, noti);
+        nm.notify(id, noti);
+    }
+
+    public void showDetail(String orderId, int type, String type2, String statusNo) {
+
+        Intent intent = DetailActivity.build(getApplicationContext(),orderId,type,type2,statusNo);
+        startActivity(intent);
     }
 }

+ 32 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -22,12 +22,14 @@ import android.support.v4.app.ActivityCompat;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.RelativeLayout;
 
 import com.usai.redant.apexdrivers.base.BasicActivity;
+import com.usai.redant.apexdrivers.detail.DetailActivity;
 import com.usai.redant.apexdrivers.home.HomeFragment;
 import com.usai.redant.apexdrivers.login.LoginFragment;
 import com.usai.redant.apexdrivers.network.Network;
@@ -36,6 +38,9 @@ import com.usai.redant.rautils.utils.ImageUtil;
 import com.usai.redant.rautils.utils.RAUtil;
 import com.usai.redant.rautils.utils.dbgUtil;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
 
 public class MainActivity extends BasicActivity implements LoginFragment.LoginCallBack {
 
@@ -135,6 +140,33 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
         Test();
 
         checkPermissions();
+
+        if (ApexDriverApplication.sharedApplication().isLogin()) {
+
+            Intent intent = getIntent();
+            if (intent != null) {
+                String apsStr = intent.getStringExtra("aps");
+                if (!TextUtils.isEmpty(apsStr)) {
+
+                    try {
+                        JSONObject aps = new JSONObject(apsStr);
+
+                        final String orderID = aps.optString("order-id");
+                        final int orderType = aps.optInt("order-type");
+                        final String orderType2 = aps.optString("order-type2");
+                        final String statusNo = aps.optString("status-no");
+
+                        Intent detailIntent = DetailActivity.build(getApplicationContext(),orderID,orderType,orderType2,statusNo);
+                        startActivity(detailIntent);
+
+                    } catch (JSONException e) {
+                        e.printStackTrace();
+                    }
+
+                }
+            }
+
+        }
     }
 
     void checkPowerManagement()

+ 36 - 2
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/receiver/ApexDriverAlarmReceiver.java

@@ -11,8 +11,14 @@ import android.util.Log;
 import com.usai.redant.apexdrivers.ApexDriverApplication;
 import com.usai.redant.rautils.receiver.AlarmReceiver;
 import com.usai.redant.rautils.receiver.RABroadcast;
+import com.usai.redant.rautils.utils.Network;
 import com.usai.redant.rautils.utils.RAUtil;
 
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import static com.usai.redant.rautils.utils.Network.RESULT_TRUE;
+
 public class ApexDriverAlarmReceiver extends AlarmReceiver {
     //负责通过alarm 从后台取notification 列表
 
@@ -147,8 +153,36 @@ public class ApexDriverAlarmReceiver extends AlarmReceiver {
     {
 
 
-        String dev_id = RAUtil.getDeviceId(context);
-        Log.d("ApexDriverAlarmReceiver", "check_push: not implementation"+dev_id);
+//        String dev_id = RAUtil.getDeviceId(context);
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+
+                JSONObject json = com.usai.redant.apexdrivers.network.Network.pullNotification();
+
+                if (json != null) {
+
+                    int result = json.optInt("result",0);
+                    if (result == RESULT_TRUE) {
+
+                        JSONArray notifications = json.optJSONArray("notifications");
+                        if (notifications != null) {
+
+                            for (int i = 0; i < notifications.length(); i++) {
+                                JSONObject notification = notifications.optJSONObject(i);
+                                if (notification != null) {
+                                    ApexDriverApplication.sharedApplication().receiveNotification(notification);
+                                }
+                            }
+
+                        }
+
+                    }
+
+                }
+
+            }
+        }).start();
 
 
         return ;