public classMyReceiver extends BroadcastReceiver {private static final String TAG = MyReceiver.class.getSimpleName();private static final int NOTIFICATION_SHOW_SHOW_AT_MOST = 3; //推送通知最多显示条数
@Overridepublic voidonReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();// if(intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)){
Log.i(TAG,"接收到了通知");
String title=bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
String content=bundle.getString(JPushInterface.EXTRA_ALERT);
String extra=bundle.getString(JPushInterface.EXTRA_EXTRA);
Log.i(TAG,"标题:【"+title+"】,内容:【"+content+"】,附加参数:【"+extra+"】");
}else if(intent.getAction().equals(JPushInterface.ACTION_MESSAGE_RECEIVED)){
Log.i(TAG,"接收到了消息");
String message=bundle.getString(JPushInterface.EXTRA_MESSAGE);
processCustomMessage(context, bundle);
Log.i(TAG,"接收到的消息是:【"+message+"】");
}else if(intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)){
Log.i(TAG,"用户正在打开通知");
}
}/**
* 实现自定义推送声音
* @param context
* @param bundle*/
private voidprocessCustomMessage(Context context, Bundle bundle) {
NotificationCompat.Builder notification= newNotificationCompat.Builder(context);
String title=bundle.getString(JPushInterface.EXTRA_TITLE);
String msg=bundle.getString(JPushInterface.EXTRA_MESSAGE);
String extras=bundle.getString(JPushInterface.EXTRA_EXTRA);
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon_mdpi);
Intent mIntent= new Intent(context,****.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.icon_mdpi)
.setLargeIcon(bitmap)
.setNumber(NOTIFICATION_SHOW_SHOW_AT_MOST);
Log.e(TAG,"processCustomMessage: extras----->" +extras);if (!TextUtils.isEmpty(extras)) {try{
JSONObject extraJson= newJSONObject(extras);if (null != extraJson && extraJson.length() > 0) {
String sound= extraJson.getString("sound");if("1".equals(sound)){
notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.default_push_sound));
}else{
notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.test));
}
}
}catch(JSONException e) {
e.printStackTrace();
}
}
NotificationManager notificationManager=(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_SHOW_SHOW_AT_MOST, notification.build());//id随意,正好使用定义的常量做id,0除外,0为默认的Notification
}
}