/** * 自定义接收器 * * 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */publicclassMyReceiverextendsBroadcastReceiver{privatestaticfinalStringTAG="MyReceiver";@OverridepublicvoidonReceive(Contextcontext,Intentintent){Bundlebundle=intent.getExtras();Log.d(TAG,"onReceive - "+intent.getAction()+", extras: "+printBundle(bundle));if(JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())){StringregId=bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Log.d(TAG,"接收Registration Id : "+regId);//send the Registration Id to your server...}elseif(JPushInterface.ACTION_UNREGISTER.equals(intent.getAction())){StringregId=bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Log.d(TAG,"接收UnRegistration Id : "+regId);//send the UnRegistration Id to your server...}elseif(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){Log.d(TAG,"接收到推送下来的自定义消息: "+bundle.getString(JPushInterface.EXTRA_MESSAGE));}elseif(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){Log.d(TAG,"接收到推送下来的通知");intnotifactionId=bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);Log.d(TAG,"接收到推送下来的通知的ID: "+notifactionId);}elseif(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){Log.d(TAG,"用户点击打开了通知");//打开自定义的ActivityIntenti=newIntent(context,TestActivity.class);i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(i);}else{Log.d(TAG,"Unhandled intent - "+intent.getAction());}}// 打印所有的 intent extra 数据privatestaticStringprintBundle(Bundlebundle){StringBuildersb=newStringBuilder();for(Stringkey:bundle.keySet()){if(key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)){sb.append("nkey:"+key+", value:"+bundle.getInt(key));}else{sb.append("nkey:"+key+", value:"+bundle.getString(key));}}returnsb.toString();}}
参数说明 Context context 应用的ApplicationContext Set days 0表示星期天,1表示星期一,以此类推。 (7天制,Set集合里面的int范围为0到6) Sdk1.2.9 – 新功能:set的值为null,则任何时间都可以收到消息和通知,set的size为0,则表示任何时间都收不到消息和通知. int startHour 允许推送的开始时间 (24小时制:startHour的范围为0到23) int endHour 允许推送的结束时间 (24小时制:endHour的范围为0到23)