Android采用自动集成的极光,能收到推送通知和点击打开通知内容,发送自定义通知收不到怎么回事呢?
package com.reada.readahd.jiguang;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.reada.readahd.R;
import com.reada.readahd.ui.activity.TestActivity;
import com.reada.readahd.utils.LogUtil;
import com.reada.readahd.utils.ToastUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.helper.Logger;
import static android.content.Context.NOTIFICATION_SERVICE;
/**
* 自定义接收器
* <p>
* 如果不定义这个 Receiver,则:
* 1) 默认用户会打开主界面
* 2) 接收不到自定义消息
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JIGUANG";
private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3; //推送通知最多显示条数
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
Logger.d(TAG, "极光接收器 " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Logger.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);
ToastUtils.showToast("自定义消息收到啦");
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Logger.d(TAG, "[MyReceiver] 用户点击打开了通知");
openNotification(context, bundle);
// //解析json
// String jsonData = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);//json串
// String type = bundle.getString(JPushInterface.EXTRA_ALERT);//type
// LogUtil.e("极光通知", "内容类型" + type);
// LogUtil.e("jsonData", "内容extras" + jsonData);
//
// JSONObject object = new JSONObject(jsonData);
// String contents = object.getString("content");
String extras = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);//type
LogUtil.e("极光通知打开", "内容类型extras" + extras);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Logger.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Logger.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
} else {
Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
} catch (Exception e) {
}
}
//打开通知
private void openNotification(Context context, Bundle bundle) {
String type = bundle.getString(JPushInterface.EXTRA_ALERT);//type
Intent intent = new Intent();//1,上传作品。2,收藏,3,点赞 4,关注 6,消息。7,反馈
switch (type) {
case "1":
intent.setClass(context, TestActivity.class);
intent.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
break;
case "2":
intent.setClass(context, TestActivity.class);
intent.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
break;
case "3":
intent.setClass(context, TestActivity.class);
intent.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
break;
case "4":
intent.setClass(context, TestActivity.class);
intent.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
break;
case "7":
intent.setClass(context, TestActivity.class);
intent.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
break;
default:
break;
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
Logger.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();
while (it.hasNext()) {
String myKey = it.next();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " + json.optString(myKey) + "]");
}
} catch (JSONException e) {
Logger.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.get(key));
}
}
return sb.toString();
}
//send msg to MainActivity
private void processCustomMessage(Context context, Bundle bundle) {
String channelID = "1";
String channelName = "channel_name";
// NotificationCompat.Builder notification = new NotificationCompat.Builder(context);
//
String type = bundle.getString(JPushInterface.EXTRA_ALERT);//1,上传作品。2,收藏,3,点赞 4,关注 6,消息。7,反馈
// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
// String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
// Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.reada_icon);
// LogUtil.e("极光通知extras", "内容extras" + extras);
// switch (type) {
// case "1":
// notification .setContentTitle("我是标题1");
// notification.setContentText("我是谁");
// break;
// default:
// notification .setContentTitle("我是未知标题1");
// notification.setContentText("我不知道你");
// break;
// }
// Intent mIntent = new Intent(context, TestActivity.class);
// mIntent.putExtras(bundle);
// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
// notification.setContentIntent(pendingIntent);
// .setAutoCancel(true)
// .setContentText(msg)
// .setContentTitle(title.equals("") ? "title" : title)
// .setSmallIcon(R.mipmap.reada_icon)
// .setLargeIcon(bitmap)
// .setNumber(NOTIFICATION_SHOW_SHOW_AT_MOST);
//
// NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
// notificationManager.notify(NOTIFICATION_SHOW_SHOW_AT_MOST, notification.build()); //id随意,正好使用定义的常量做id,0除外,0为默认的Notification
// if (!TextUtils.isEmpty(title)) {
// try {
// JSONObject extraJson = new JSONObject(title);
// if (extraJson.length() > 0) {
// String content = "";
// try {
// JSONObject jsonObject = new JSONObject(title);
// content = jsonObject.getString("content");
// notification.setContentTitle(content);
// notification.setContentText(content);
// Log.d(TAG, "极光content" + content);
//
//
// } catch (JSONException e) {
// e.printStackTrace();
// }
// }
// } catch (JSONException e) {
// e.printStackTrace();
// Log.d(TAG, "push json is fail");
// }
//
// }
// 跳转的Activity
// Intent intent = new Intent(context, LoginActivity.class);
// intent.putExtras(bundle);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// 获得系统推送的自定义消息
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
//适配安卓8.0的消息渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, channelID);
switch (type) {
case "1":
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title1")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
break;
case "2":
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title2")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
break;
case "3":
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title3")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
break;
case "4":
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title4")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
break;
default:
notification.setAutoCancel(true)
.setContentText(message)
.setContentTitle("我是Title")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
break;
}
notificationManager.notify((int) (System.currentTimeMillis() / 1000), notification.build());
}
}