1.注册
<receiver android:name=".ui.MyJPushReceiver" android:enabled="true"><intent-filter><action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /><action android:name="cn.jpush.android.intent.REGISTRATION" /><action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /><action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /><category android:name="APPLICATIONID" /></intent-filter> </receiver>2.代码
public class MyJPushReceiver extends BroadcastReceiver {@Override public void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { // Log.d("MyJPushReceiver", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); // Log.d("MyJPushReceiver", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_TITLE)); // Log.d("MyJPushReceiver", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); // Log.d("MyJPushReceiver", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE)); // Log.d("MyJPushReceiver", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_EXTRA)); processCustomMessage(context,bundle);return;} }private void processCustomMessage(Context context,Bundle bundle){NotificationManager manger=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);//为了版本兼容 选择V7包下的NotificationCompat进行构造 NotificationCompat.Builder builder = new NotificationCompat.Builder(context);//Ticker是状态栏显示的提示 builder.setTicker(bundle.getString(JPushInterface.EXTRA_TITLE));//第一行内容 通常作为通知栏标题 builder.setContentTitle(bundle.getString(JPushInterface.EXTRA_TITLE));//第二行内容 通常是通知正文 builder.setContentText(bundle.getString(JPushInterface.EXTRA_MESSAGE));//可以点击通知栏的删除按钮删除 builder.setAutoCancel(true);//系统状态栏显示的小图标 builder.setSmallIcon(R.drawable.ic_launcher_icon);Notification notification = builder.build();notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.push_notification_price_sound);builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE|NotificationCompat.DEFAULT_LIGHTS);notification.flags |= Notification.FLAG_AUTO_CANCEL;Intent clickIntent = new Intent(); //点击通知之后要发送的广播 int id = (int) (System.currentTimeMillis() / 1000);clickIntent.addCategory(GbankerApplication.getAppPackageName(context));clickIntent.setAction(JPushInterface.ACTION_NOTIFICATION_OPENED);clickIntent.putExtra(JPushInterface.EXTRA_EXTRA,bundle.getString(JPushInterface.EXTRA_EXTRA));PendingIntent contentIntent = PendingIntent.getBroadcast(context, id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);notification.contentIntent = contentIntent;manger.notify(id,notification);}}