Просмотр исходного кода

1.修改Android Apex Drivers推送通知显示。

Pen Li 7 лет назад
Родитель
Сommit
548c4140c5

+ 51 - 4
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -2,6 +2,10 @@ package com.usai.redant.apexdrivers;
 
 import android.app.Activity;
 import android.app.Application;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -14,6 +18,7 @@ import android.location.Location;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.v4.app.NotificationCompat;
 import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
 import android.util.Log;
@@ -25,6 +30,8 @@ import com.usai.redant.rautils.receiver.RABroadcast;
 import com.usai.redant.rautils.utils.AESUtil;
 import com.usai.redant.rautils.utils.RAUtil;
 
+import org.json.JSONObject;
+
 import java.lang.reflect.Field;
 
 public class ApexDriverApplication extends Application {
@@ -141,10 +148,10 @@ public class ApexDriverApplication extends Application {
 
             loadBackgroundReportType();
 
-            if (user != null) {
-                // 上传Token
-                Network.uploadToken(RAUtil.getDeviceId(getApplicationContext()));
-            }
+//            if (user != null) {
+//                // 上传Token
+//                Network.uploadToken(RAUtil.getDeviceId(getApplicationContext()));
+//            }
 
             Log.d("ApexDriverApplication", "onCreate: u:"+user+" p:"+password);
             mServiceConnection = new ServiceConnection() {
@@ -538,4 +545,44 @@ public class ApexDriverApplication extends Application {
             break;
         }
     }
+
+    public void receiveNotification(JSONObject notification) {
+
+    }
+
+    public void popLocalNotification(String title, String msg) {
+
+
+        Intent intent = new Intent();
+        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
+
+        //1.获取系统通知的管理者
+        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+
+        Notification noti = null;
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+
+            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
+            nm.createNotificationChannel(channel);
+
+            noti = new NotificationCompat.Builder(this, CHANNEL_ID)
+                    .setContentTitle("我是大的标题")
+                    .setContentText("我是内容")
+                    .setSmallIcon(R.drawable.icon_50)
+                    .setContentIntent(contentIntent)
+                    .build();
+        } else {
+
+            noti = new Notification.Builder(this)
+                    .setContentTitle("我是大的标题")
+                    .setContentText("我是内容")
+                    .setSmallIcon(R.drawable.icon_50)
+                    .setContentIntent(contentIntent)
+                    .build();
+
+        }
+
+        nm.notify(1, noti);
+    }
 }

+ 4 - 3
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/network/Network.java

@@ -38,6 +38,8 @@ public class Network extends com.usai.redant.rautils.utils.Network {
     public static final String URL_UPLOAD_TOKEN = URL_HOST + "/j/mobile/uploadToken.mo/";
     public static final String URL_PULL_NOTIFICATION = URL_HOST + "/j/mobile/pullNotification.mo/";
 
+    public static final int PUSH_TIME_OUT_INTERVAL = 5 * 1000;
+
     private static void prepareParams(Bundle params) {
         if (params == null) {
             params = new Bundle();
@@ -369,7 +371,7 @@ public class Network extends com.usai.redant.rautils.utils.Network {
     }
 
     /**
-     * @brief 上传推送令牌,在登陆成功的时候 以及 程序启动时自动登陆的情况下
+     * @brief 上传推送令牌,在登陆成功的时候
      * */
     public static JSONObject uploadToken(String token) {
 
@@ -397,8 +399,7 @@ public class Network extends com.usai.redant.rautils.utils.Network {
 
         prepareParams(params);
 
-        int timeout = 5 * 1000;
-        String jsonStr = getJSON(URL_UPLOAD_TOKEN,params,timeout);
+        String jsonStr = getJSON(URL_UPLOAD_TOKEN,params,PUSH_TIME_OUT_INTERVAL);
 
         return handleJson(jsonStr);
     }