hanjing 7 år sedan
förälder
incheckning
3b6cae84e8

+ 5 - 1
app/src/main/AndroidManifest.xml

@@ -969,7 +969,11 @@
             android:name=".provider.TokenRenewalProvider"
             android:authorities="${applicationId}.renewal"
             android:enabled="true"
-            android:exported="true"></provider>
+            android:exported="true" />
+
+        <service
+            android:name=".service.PushMessageService"
+            android:exported="false"></service>
     </application>
 
 </manifest>

+ 4 - 3
app/src/main/java/com/sheep/gamegroup/receiver/SheepJpushReceiver.java

@@ -1,5 +1,6 @@
 package com.sheep.gamegroup.receiver;
 
+import android.app.Activity;
 import android.app.Notification;
 import android.app.NotificationChannel;
 import android.app.NotificationManager;
@@ -18,6 +19,7 @@ import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.view.activity.MiddleAct;
 import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.service.PushMessageService;
 
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -52,7 +54,7 @@ public class SheepJpushReceiver extends BroadcastReceiver {
 
             } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {//自定义消息中有自定义字段时才会接收到,且不会有通知
                 Log.d(TAG, "[SheepJpushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
-                processCustomMessage(context, bundle, intent.getIntExtra(JPushInterface.EXTRA_MSG_ID, new Random().nextInt(113920)));
+                processCustomMessage(context, intent);
 
             } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//除了自定义消息中有自定义字段的消息,都会接收到通知
                 Log.d(TAG, "[SheepJpushReceiver] 接收到推送下来的通知");
@@ -117,8 +119,7 @@ public class SheepJpushReceiver extends BroadcastReceiver {
     /**
      * 处理自定义消息
      * @param context
-     * @param bundle
-     * @param msgId
+     * @param intent
      * @throws JSONException
      */
     private void processCustomMessage(Context context, Bundle bundle, int msgId) throws JSONException {

+ 163 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/service/PushMessageService.java

@@ -0,0 +1,163 @@
+package com.sheep.jiuyan.samllsheep.service;
+
+import android.app.IntentService;
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.PowerManager;
+import android.support.v4.app.NotificationCompat;
+import android.text.TextUtils;
+
+import com.sheep.gamegroup.module.skin.util.SkinUtil;
+import com.sheep.gamegroup.util.LogUtil;
+import com.sheep.gamegroup.view.activity.MiddleAct;
+import com.sheep.jiuyan.samllsheep.BuildConfig;
+import com.sheep.jiuyan.samllsheep.R;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.util.Random;
+
+import cn.jpush.android.api.JPushInterface;
+
+
+/**
+ * An {@link IntentService} subclass for handling asynchronous task requests in
+ * a service on a separate handler thread.
+ * <p>
+ * TODO: Customize class - update intent actions and extra parameters.
+ */
+public class PushMessageService extends IntentService {
+
+    private static final Object LOCK = PushMessageService.class;
+    private static PowerManager.WakeLock sWakeLock;
+    private static final String WAKELOCK_KEY = "GCM_LIB";
+
+    public PushMessageService() {
+        super("PushMessageService");
+    }
+
+    @Override
+    protected void onHandleIntent(Intent intent) {
+        try {
+            handle(intent);
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void handle(Intent intent) throws JSONException {
+        Bundle bundle = intent.getExtras();
+        int msgId = intent.getIntExtra(JPushInterface.EXTRA_MSG_ID, new Random().nextInt(113920));
+        //        JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
+//        String msg = json.optString("msg");
+//        String title = json.optString("title");
+//        String id = "my_channel_01";
+//        String name = context.getString(R.string.app_name);
+//        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
+//        if(notificationManager != null) {
+//            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id)
+//                    .setContentTitle(title)
+//                    .setContentText(msg);
+//            Notification notification;
+//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+//                NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
+//                notificationManager.createNotificationChannel(mChannel);
+//            } else {
+//                notificationBuilder.setOngoing(true);
+//            }
+//            notification = notificationBuilder.build();
+//            notificationManager.notify(msgId, notification);
+//        }
+        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
+        if (mNotificationManager != null) {
+            String appName = this.getString(R.string.app_name);
+            String summaryText = "来自" + appName;
+            JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
+            String msg = json.optString("msg");
+            String title = json.optString("title");
+            //为了版本兼容  选择V7包下的NotificationCompat进行构造
+            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, String.valueOf(msgId));
+            //点击通知栏跳转到相应的应用里面
+            Intent notificationIntent = new Intent(this, MiddleAct.class);
+            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+            notificationIntent.putExtras(bundle);
+
+            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+
+            notificationBuilder
+                    //下拉显示的大图标
+                    //                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), SkinUtil.getAppIcon()))
+                    //                .setSmallIcon(R.mipmap.remenyx)//系统状态栏显示的小图标 remenyx
+                    .setSmallIcon(SkinUtil.getAppIcon())
+                    .setTicker(title)
+                    .setDefaults(Notification.DEFAULT_VIBRATE)
+                    .setWhen(System.currentTimeMillis())
+                    .setTicker(title)//Ticker是状态栏显示的提示
+                    .setDefaults(NotificationCompat.DEFAULT_ALL);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+                notificationBuilder.setContentTitle(title)//第一行内容  通常作为通知栏标题
+                        .setContentText(msg);//第二行内容 通常是通知正文
+                NotificationChannel channel = new NotificationChannel(String.valueOf(msgId), appName, NotificationManager.IMPORTANCE_HIGH);
+                channel.canBypassDnd();//是否可以绕过请勿打扰模式
+                channel.canShowBadge();//是否可以显示icon角标
+                channel.enableLights(true);//是否显示通知闪灯
+                channel.enableVibration(true);//收到小时时震动提示
+                channel.setBypassDnd(true);//设置绕过免打扰
+                channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_SECRET);
+                channel.setLightColor(this.getColor(R.color.theme_app_main));//设置闪光灯颜色
+                channel.getAudioAttributes();//获取设置铃声设置
+                channel.setVibrationPattern(new long[]{100, 200, 100});//设置震动模式
+                channel.shouldShowLights();//是否会闪光
+                mNotificationManager.createNotificationChannel(channel);
+            } else if (BuildConfig.DEBUG) {
+                notificationBuilder.setContentTitle(title)//第一行内容  通常作为通知栏标题
+                        .setContentText(msg);//第二行内容 通常是通知正文
+            } else {//下面部分为测试部分
+                notificationBuilder.setContentTitle(appName)//第一行内容  通常作为通知栏标题
+                        .setContentText(title);//第二行内容 通常是通知正文
+                if (!TextUtils.isEmpty(msg)) {
+                    notificationBuilder.setSubText(msg);//第三行内容 通常是内容摘要什么的 在低版本机器上不一定显示
+                    notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).setSummaryText(summaryText).bigText(msg));
+                } else {
+                    //           notificationBuilder.setSubText("subText");
+                    //添加宽视图
+                    NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
+                    style.setBigContentTitle(title);
+                    //由手机屏幕像素决定显示多少
+                    //          style.addLine("line1");
+                    //          style.addLine("line2");
+                    style.setSummaryText(summaryText);//添加概要
+                    notificationBuilder.setStyle(style);
+                }
+            }
+            notificationBuilder.setPriority(Notification.PRIORITY_MAX).setContentIntent(pendingIntent);//点击跳转的intent
+            Notification notification = notificationBuilder.build();
+            notification.flags |= Notification.FLAG_AUTO_CANCEL;
+            mNotificationManager.notify(msgId, notification);
+        }
+    }
+
+    public static void runIntentInService(Context context, Intent intent) {
+        synchronized (LOCK) {
+            if (sWakeLock == null) {
+                // This is called from BroadcastReceiver, there is no init.
+                PowerManager pm = (PowerManager)
+                        context.getSystemService(Context.POWER_SERVICE);
+                sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+                        WAKELOCK_KEY);
+            }
+        }
+        LogUtil.logI("Acquiring wakelock");
+        sWakeLock.acquire();
+        intent.setClassName(context, "com.sheep.jiuyan.samllsheep.service.PushMessageService");
+        context.startService(intent);
+    }
+
+}