Android 收到推送的消息的改變嫩舟,
在收到通知的時(shí)候無法改變通知欄顯示的時(shí)候i缚够,一般通知在Android上只顯示一行杭朱,此時(shí)我們?cè)撛趺窗婺兀?/p>
在經(jīng)過好長(zhǎng)時(shí)間的不斷查閱之下术唬,做出了自己想要的效果抑诸,在手到推銷消息的時(shí)候能狗多行顯示烂琴,
在無奈之下,只好將通知改為自定義消息了蜕乡,話不多說奸绷,直接上代碼:
public class MyReceiver extends BroadcastReceiver{
private static finalStringTAG="JIGUANG-Example";
@Override
public voidonReceive(Contextcontext,Intentintent) {
try{
Bundle bundle=intent.getExtras();
if(JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId=bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
//send the Registration Id to your server...
}else if(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG,"接收到推送下來的自定義消息: "+bundle.getString(JPushInterface.EXTRA_MESSAGE));
receivingNotification(context,bundle);
}else if(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG,"接收到推送下來的通知");
intnotifactionId=bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG,"接收到推送下來的通知的ID: "+notifactionId);
receivingNotification(context,bundle);
}else if(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG,"用戶點(diǎn)擊打開了通知");
//打開自定義的Activity
Intent i=newIntent(context,TestActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}else if(JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG,"用戶收到到RICH PUSH CALLBACK: "+bundle.getString(JPushInterface.EXTRA_EXTRA));
//在這里根據(jù)JPushInterface.EXTRA_EXTRA的內(nèi)容處理代碼,比如打開新的Activity层玲, 打開一個(gè)網(wǎng)頁等..
}else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
booleanconnected=intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE,false);
Log.w(TAG,""+intent.getAction()+" connected state change to "+connected);
}else{
}
}catch(Exceptione) {
}
}
//打印所有的intent extra數(shù)據(jù)
private staticString printBundle(Bundlebundle) {
StringBuilder sb=newStringBuilder();
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))) {
Log.i(TAG,"This message has no Extra data");
continue;
}
try{
JSONObject json=newJSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iteratorit=json.keys();
while(it.hasNext()) {
String myKey=it.next();
sb.append("\nkey:"+key+", value: ["+
myKey+" - "+json.optString(myKey)+"]");
}
}catch(JSONExceptione) {
Log.e(TAG,"Get message extra JSON error!");
}
}else{
sb.append("\nkey:"+key+", value:"+bundle.getString(key));
}returnsb.toString();}
//想要改變自定義消息的時(shí)候号醉,這里是關(guān)鍵
@RequiresApi(api=Build.VERSION_CODES.JELLY_BEAN)
private voidreceivingNotification(Contextcontext,Bundlebundle) {
String message=bundle.getString(JPushInterface.EXTRA_MESSAGE);
RemoteViews customView=newRemoteViews(context.getPackageName(),R.layout.notifycation_layout);
//最終要的就是布局,如果是想要通知藍(lán)里變的內(nèi)容隨之變化的話称簿,就必須要給最外層的布局高度給一個(gè)定值布局在后邊
customView.setTextViewText(“自定義布局里邊顯示的title”,"title");
//例如 customView.setTextViewText(R.id.titlet,""這里是標(biāo)題);
NotificationCompat.Builder mBuilder=newNotificationCompat.Builder(context);
mBuilder.setContent(customView)
.setPriority(Notification.PRIORITY_DEFAULT)
.setOngoing(false)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notify=mBuilder.build();
notify.bigContentView=customView;
notify.flags|=Notification.FLAG_AUTO_CANCEL;//滑動(dòng)通知后通知欄消失
//通知id需要不唯一扣癣,要不然會(huì)覆蓋前一條通知
intnotifyId=(int)System.currentTimeMillis();
NotificationManager manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notifyId,notify);
}}
這里展示布局
布局這里要給定值,至于要多高自己設(shè)置