你好,我是Android自动集成的,极光后台推送的通知,自定义都能收到,我们自己服务器根据别名推送的收不到推送,下面是后台服务器端的代码,我们需要自定义通知栏,alert已经设为空,但是收不到任何通知呢
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(users))
.setNotification(Notification.newBuilder()
.setAlert("")
.addPlatformNotification(AndroidNotification.newBuilder()
.setAlert("")
.setTitle("Android Title")
.addExtras(extras)
.addExtra("data", content)
.addExtra("type", type)
.addExtra("jsonExtra", jsonExtra)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra("extra_key", "extra_value").build())
.build())
.build();
上面是后台服务器端代码
下面是Android端代码
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));
LogUtil.e("我是自定义", extras);
processCustomMessage2(context, bundle);
} 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] 用户点击打开了通知");
String alert = bundle.getString(JPushInterface.EXTRA_ALERT);
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
LogUtil.e("我是点开的", "msg :" + message + "alert :" + alert + " json串:" + extras + "标题" + title);
openNotification(context, bundle);
} 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 extras = bundle.getString(JPushInterface.EXTRA_EXTRA);//type
String type = "";
if (!TextUtils.isEmpty(extras)) {
try {
JSONObject extraJson = new JSONObject(extras);
if (extraJson.length() > 0) {
try {
JSONObject jsonObject = new JSONObject(extras);
type = jsonObject.getString("type");
Log.d(TAG, "type类型是打开" + type);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, "push json is fail");
}
}
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 "6":
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";
// 获得系统推送的自定义消息
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.reada_icon);
Intent mIntent = new Intent(context, TestActivity.class);
// ThirdView.isReadList = false;
mIntent.putExtras(bundle);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
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);
notification.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setContentText("我是内容")
.setContentTitle("自定义Title")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.test));
notificationManager.notify((int) (System.currentTimeMillis() / 1000), notification.build());
}
private ArrayList<JGBean> mList = new ArrayList<>();
private void processCustomMessage2(Context context, Bundle bundle) {
// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras_pross = bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.d(TAG, "收到了自定义消息@@消息extra是extras:" + extras_pross);
String channelID = "1";
String channelName = "channel_name";
if (!TextUtils.isEmpty(extras_pross)) {
try {
JSONObject extraJson = new JSONObject(extras_pross);
if (extraJson.length() > 0) {
String type_pross = "";
try {
JSONObject jsonObject = new JSONObject(extras_pross);
type_pross = jsonObject.getString("type");
Log.d(TAG, "type类型是" + type_pross);
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);
notification.setAutoCancel(true)
.setContentText("666666")
.setContentTitle("我是777777")
.setSmallIcon(R.drawable.icon_audio)
.setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify((int) (System.currentTimeMillis() / 1000), notification.build());
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, "push json is fail");
}
}
}
}