|
@@ -1,21 +1,20 @@
|
|
|
package com.sheep.gamegroup.receiver;
|
|
package com.sheep.gamegroup.receiver;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
import android.app.Notification;
|
|
|
|
|
+import android.app.NotificationChannel;
|
|
|
import android.app.NotificationManager;
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
import android.app.PendingIntent;
|
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.BroadcastReceiver;
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
import android.content.Intent;
|
|
|
-import android.graphics.BitmapFactory;
|
|
|
|
|
|
|
+import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
import android.text.TextUtils;
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
import com.sheep.gamegroup.util.Jump2View;
|
|
import com.sheep.gamegroup.util.Jump2View;
|
|
|
import com.sheep.gamegroup.view.activity.MiddleAct;
|
|
import com.sheep.gamegroup.view.activity.MiddleAct;
|
|
|
-import com.sheep.gamegroup.view.activity.SplashAct;
|
|
|
|
|
import com.sheep.jiuyan.samllsheep.R;
|
|
import com.sheep.jiuyan.samllsheep.R;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONException;
|
|
@@ -26,6 +25,8 @@ import java.util.Random;
|
|
|
|
|
|
|
|
import cn.jpush.android.api.JPushInterface;
|
|
import cn.jpush.android.api.JPushInterface;
|
|
|
|
|
|
|
|
|
|
+import static android.content.Context.NOTIFICATION_SERVICE;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Created by realicing on 2018/5/9.
|
|
* Created by realicing on 2018/5/9.
|
|
|
* realicing@sina.com
|
|
* realicing@sina.com
|
|
@@ -51,7 +52,7 @@ public class SheepJpushReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {//自定义消息中有自定义字段时才会接收到,且不会有通知
|
|
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {//自定义消息中有自定义字段时才会接收到,且不会有通知
|
|
|
Log.d(TAG, "[SheepJpushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
|
|
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())) {//除了自定义消息中有自定义字段的消息,都会接收到通知
|
|
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//除了自定义消息中有自定义字段的消息,都会接收到通知
|
|
|
Log.d(TAG, "[SheepJpushReceiver] 接收到推送下来的通知");
|
|
Log.d(TAG, "[SheepJpushReceiver] 接收到推送下来的通知");
|
|
@@ -117,52 +118,82 @@ public class SheepJpushReceiver extends BroadcastReceiver {
|
|
|
* 处理自定义消息
|
|
* 处理自定义消息
|
|
|
* @param context
|
|
* @param context
|
|
|
* @param bundle
|
|
* @param bundle
|
|
|
|
|
+ * @param msgId
|
|
|
* @throws JSONException
|
|
* @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);
|
|
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);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|