Procházet zdrojové kódy

兼容8.0以上手机显示通知栏

zengjiebin před 7 roky
rodič
revize
fc8a35081b

+ 79 - 48
app/src/main/java/com/sheep/gamegroup/receiver/SheepJpushReceiver.java

@@ -1,21 +1,20 @@
 package com.sheep.gamegroup.receiver;
 
 import android.app.Notification;
+import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
-import android.graphics.BitmapFactory;
+import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.NotificationCompat;
 import android.text.TextUtils;
 import android.util.Log;
 
-
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.view.activity.MiddleAct;
-import com.sheep.gamegroup.view.activity.SplashAct;
 import com.sheep.jiuyan.samllsheep.R;
 
 import org.json.JSONException;
@@ -26,6 +25,8 @@ import java.util.Random;
 
 import cn.jpush.android.api.JPushInterface;
 
+import static android.content.Context.NOTIFICATION_SERVICE;
+
 /**
  * Created by realicing on 2018/5/9.
  * realicing@sina.com
@@ -51,7 +52,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);
+                processCustomMessage(context, bundle, intent.getIntExtra(JPushInterface.EXTRA_MSG_ID, new Random().nextInt(113920)));
 
             } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//除了自定义消息中有自定义字段的消息,都会接收到通知
                 Log.d(TAG, "[SheepJpushReceiver] 接收到推送下来的通知");
@@ -117,52 +118,82 @@ public class SheepJpushReceiver extends BroadcastReceiver {
      * 处理自定义消息
      * @param context
      * @param bundle
+     * @param msgId
      * @throws JSONException
      */
-    private void processCustomMessage(Context context, Bundle bundle) throws JSONException {
-        int id = new Random().nextInt(113920);
-//        bundle.putInt("android_notification_id", id);
-        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(context, "" + id);
-        //点击通知栏跳转到相应的应用里面
-        Intent notificationIntent = new Intent(context, MiddleAct.class);
-        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
-        notificationIntent.putExtras(bundle);
-
-        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-        notificationBuilder
-                //下拉显示的大图标
-//                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))
-//                .setSmallIcon(R.mipmap.remenyx)//系统状态栏显示的小图标 remenyx
-                .setSmallIcon(R.drawable.icon)
-                .setWhen(System.currentTimeMillis())
-                .setTicker(title)//Ticker是状态栏显示的提示
-                .setContentTitle("小绵羊")//第一行内容  通常作为通知栏标题
-                .setContentText(title)//第二行内容 通常是通知正文
-                .setOngoing(true)//禁止滑动删除
-                .setDefaults(NotificationCompat.DEFAULT_ALL);
-        if (!TextUtils.isEmpty(msg)) {
-            notificationBuilder.setSubText(msg);//第三行内容 通常是内容摘要什么的 在低版本机器上不一定显示
-            notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).setSummaryText("来自:小绵羊").bigText(msg));
-        } else {
-//           notificationBuilder.setSubText("subText");
-            //添加宽视图
-            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
-            style.setBigContentTitle(title);
-            //由手机屏幕像素决定显示多少
-//          style.addLine("line1");
-//          style.addLine("line2");
-            style.setSummaryText("来自:小绵羊");//添加概要
-            notificationBuilder.setStyle(style);
-        }
-        notificationBuilder.setPriority(Notification.PRIORITY_MAX).setContentIntent(pendingIntent);//点击跳转的intent
-        Notification notification = notificationBuilder.build();
-        notification.flags |= Notification.FLAG_AUTO_CANCEL;
+    private void processCustomMessage(Context context, Bundle bundle, int msgId) throws JSONException {
+//        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) context.getSystemService(Context.NOTIFICATION_SERVICE);
-        if (mNotificationManager != null)
-            mNotificationManager.notify(id, notification);
+        if (mNotificationManager != null) {
+            String appName = context.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(context, String.valueOf(msgId));
+            //点击通知栏跳转到相应的应用里面
+            Intent notificationIntent = new Intent(context, MiddleAct.class);
+            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+            notificationIntent.putExtras(bundle);
+
+            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+
+            notificationBuilder
+                    //下拉显示的大图标
+    //                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))
+    //                .setSmallIcon(R.mipmap.remenyx)//系统状态栏显示的小图标 remenyx
+                    .setSmallIcon(R.drawable.icon)
+                    .setWhen(System.currentTimeMillis())
+                    .setTicker(title)//Ticker是状态栏显示的提示
+                    .setDefaults(NotificationCompat.DEFAULT_ALL);
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+                notificationBuilder.setContentTitle(appName)//第一行内容  通常作为通知栏标题
+                        .setOngoing(true)//禁止滑动删除
+                        .setContentText(title);//第二行内容 通常是通知正文
+                NotificationChannel mChannel = new NotificationChannel(String.valueOf(msgId), appName, NotificationManager.IMPORTANCE_HIGH);
+                mNotificationManager.createNotificationChannel(mChannel);
+            } 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);
+        }
     }
 }

+ 14 - 21
app/src/main/java/com/sheep/jiuyan/samllsheep/service/ListenerShotNewService.java

@@ -11,12 +11,11 @@ import android.app.usage.UsageStatsManager;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
-import android.graphics.Color;
 import android.os.Build;
 import android.os.IBinder;
 import android.support.annotation.Nullable;
+import android.support.v4.app.NotificationCompat;
 import android.text.TextUtils;
-import android.widget.RemoteViews;
 
 import com.sheep.gamegroup.greendao.DDProviderHelper;
 import com.sheep.gamegroup.greendao.download.ScreenShotRecord;
@@ -81,23 +80,17 @@ public class ListenerShotNewService extends Service {
             manager.startListen();
         }
 
-        Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext());
 
-        String CHANNEL_ONE_ID = getPackageName()+".ListenerShotService";
-        String CHANNEL_ONE_NAME = "smallSheep";
-        NotificationChannel notificationChannel;
+        int msgId = 113922;
+        String appName = getString(R.string.app_name);
         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
-            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
-                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
-            notificationChannel.enableLights(true);
-            notificationChannel.setLightColor(Color.RED);
-            notificationChannel.setShowBadge(true);
-            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
-            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
-            if(manager != null)
-                manager.createNotificationChannel(notificationChannel);
-            notificationBuilder.setChannelId(CHANNEL_ONE_ID);
+            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+            if(mNotificationManager != null) {
+                NotificationChannel mChannel = new NotificationChannel(String.valueOf(msgId), appName, NotificationManager.IMPORTANCE_HIGH);
+                mNotificationManager.createNotificationChannel(mChannel);
+            }
         }
+        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), String.valueOf(msgId));
         //点击通知栏跳转到相应的应用里面
         Intent notificationIntent = new Intent(this, SplashAct.class);
         notificationIntent.setAction(Intent.ACTION_MAIN);
@@ -107,17 +100,17 @@ public class ListenerShotNewService extends Service {
                 notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         Notification notification = notificationBuilder.setSmallIcon(R.mipmap.remenyx)
                 .setWhen(System.currentTimeMillis())
-                .setTicker("小绵羊")
-                .setContentTitle("小绵羊正在运行")
-                .setContentText("点击回到小绵羊")
-                .setOngoing(true)
+                .setTicker(appName)
+                .setContentTitle(appName+"正在运行")
+                .setContentText("点击回到"+appName)
                 .setPriority(Notification.PRIORITY_MAX)
                 .setContentIntent(pendingIntent)
                 .setAutoCancel(false)
+                .setDefaults(NotificationCompat.DEFAULT_ALL)
                 .build();
         notification.flags |= Notification.FLAG_NO_CLEAR;
         //这里的id不能是0
-        startForeground(113922, notification);
+        startForeground(msgId, notification);
         return super.onStartCommand(intent, flags, startId);
     }
     private ScreenShotListenManager manager;