|
@@ -1,20 +1,28 @@
|
|
|
package com.sheep.gamegroup.receiver;
|
|
package com.sheep.gamegroup.receiver;
|
|
|
|
|
|
|
|
|
|
+import android.app.Notification;
|
|
|
|
|
+import android.app.NotificationManager;
|
|
|
|
|
+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.Bundle;
|
|
import android.os.Bundle;
|
|
|
|
|
+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 org.json.JSONException;
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
import java.util.Iterator;
|
|
|
|
|
+import java.util.Random;
|
|
|
|
|
|
|
|
import cn.jpush.android.api.JPushInterface;
|
|
import cn.jpush.android.api.JPushInterface;
|
|
|
|
|
|
|
@@ -22,7 +30,7 @@ import cn.jpush.android.api.JPushInterface;
|
|
|
* Created by realicing on 2018/5/9.
|
|
* Created by realicing on 2018/5/9.
|
|
|
* realicing@sina.com
|
|
* realicing@sina.com
|
|
|
* 自定义接收器
|
|
* 自定义接收器
|
|
|
- *
|
|
|
|
|
|
|
+ * <p>
|
|
|
* 如果不定义这个 Receiver,则:
|
|
* 如果不定义这个 Receiver,则:
|
|
|
* 1) 默认用户会打开主界面
|
|
* 1) 默认用户会打开主界面
|
|
|
* 2) 接收不到自定义消息
|
|
* 2) 接收不到自定义消息
|
|
@@ -59,14 +67,14 @@ public class SheepJpushReceiver extends BroadcastReceiver {
|
|
|
Log.d(TAG, "[SheepJpushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
|
|
Log.d(TAG, "[SheepJpushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
|
|
|
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
|
|
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
|
|
|
|
|
|
|
|
- } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
|
|
|
|
|
|
|
+ } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
|
|
|
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
|
|
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
|
|
|
- Log.w(TAG, "[SheepJpushReceiver]" + intent.getAction() +" connected state change to "+connected);
|
|
|
|
|
|
|
+ Log.w(TAG, "[SheepJpushReceiver]" + intent.getAction() + " connected state change to " + connected);
|
|
|
} else {
|
|
} else {
|
|
|
Log.d(TAG, "[SheepJpushReceiver] Unhandled intent - " + intent.getAction());
|
|
Log.d(TAG, "[SheepJpushReceiver] Unhandled intent - " + intent.getAction());
|
|
|
}
|
|
}
|
|
|
- } catch (Exception ignored){
|
|
|
|
|
-
|
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -77,7 +85,7 @@ public class SheepJpushReceiver extends BroadcastReceiver {
|
|
|
for (String key : bundle.keySet()) {
|
|
for (String key : bundle.keySet()) {
|
|
|
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
|
|
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
|
|
|
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
|
|
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
|
|
|
- }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
|
|
|
|
|
|
|
+ } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
|
|
|
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
|
|
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
|
|
|
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
|
|
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
|
|
|
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
|
|
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
|
|
@@ -87,12 +95,12 @@ public class SheepJpushReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
|
|
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
|
|
|
- Iterator<String> it = json.keys();
|
|
|
|
|
|
|
+ Iterator<String> it = json.keys();
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
while (it.hasNext()) {
|
|
|
String myKey = it.next();
|
|
String myKey = it.next();
|
|
|
sb.append("\nkey:" + key + ", value: [" +
|
|
sb.append("\nkey:" + key + ", value: [" +
|
|
|
- myKey + " - " +json.optString(myKey) + "]");
|
|
|
|
|
|
|
+ myKey + " - " + json.optString(myKey) + "]");
|
|
|
}
|
|
}
|
|
|
} catch (JSONException e) {
|
|
} catch (JSONException e) {
|
|
|
Log.e(TAG, "Get message extra JSON error!");
|
|
Log.e(TAG, "Get message extra JSON error!");
|
|
@@ -105,25 +113,56 @@ public class SheepJpushReceiver extends BroadcastReceiver {
|
|
|
return sb.toString();
|
|
return sb.toString();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //send msg to MainActivity
|
|
|
|
|
- private void processCustomMessage(Context context, Bundle bundle) {
|
|
|
|
|
-// if (MainActivity.isForeground) {
|
|
|
|
|
-// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
|
|
|
|
|
-// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
|
|
|
|
|
-// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
|
|
|
|
|
-// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
|
|
|
|
|
-// if (!ExampleUtil.isEmpty(extras)) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// JSONObject extraJson = new JSONObject(extras);
|
|
|
|
|
-// if (extraJson.length() > 0) {
|
|
|
|
|
-// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
|
|
|
|
|
-// }
|
|
|
|
|
-// } catch (JSONException e) {
|
|
|
|
|
-//
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// }
|
|
|
|
|
-// LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理自定义消息
|
|
|
|
|
+ * @param context
|
|
|
|
|
+ * @param bundle
|
|
|
|
|
+ * @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;
|
|
|
|
|
+ NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
+ if (mNotificationManager != null)
|
|
|
|
|
+ mNotificationManager.notify(id, notification);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|