最近在做通知欄相關(guān)功能,和微信添加好友后的通知類似。因?yàn)橄旅嬗袃蓚€(gè)view,想著應(yīng)該是自定義吧。便開始著手寫xml, 運(yùn)行后發(fā)現(xiàn)有如下問題:
1.直接 設(shè)置 builder.setCustomContentView(remoteViews)蜻直,發(fā)現(xiàn)通知欄顯示不全,此原因是自定義的remoteview 高度過高導(dǎo)致袁串。
2.用builder.setCustomBigContentView(remoteViews), 發(fā)現(xiàn)通知欄會收縮起來概而,點(diǎn)擊小三角才能展示整個(gè)通知內(nèi)容。
顯然不滿足要求囱修,經(jīng)查閱文檔看到notification 中有個(gè)addAction() 赎瑰,此方法可以在通知欄下方添加按鈕。
intent.setAction("notification_clicked_ignore");
builder.addAction(0, "忽略", PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
intent.setAction("notification_clicked_accept");
builder.addAction(0, "接受", PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
一加手機(jī)通知欄.jpg
vivo 手機(jī)通知欄.jpg
以上兩個(gè)圖片都是微信添加好友后的通知破镰,都是是系統(tǒng)自帶的通知欄餐曼,只是在不同的手機(jī)上表現(xiàn)不同。
需要注意的是在一加手機(jī)忽略和接受是靠近左邊的鲜漩,而在vivo 手機(jī)上的表現(xiàn)是左右均分源譬,如希望在一加手機(jī)上也左右均分,則可以用builder.setFullScreenIntent(). 代碼如下
//如果描述的PendingIntent已經(jīng)存在孕似,則在產(chǎn)生新的Intent之前會先取消掉當(dāng)前的
PendingIntent hangPendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);
一加手機(jī)下方按鈕均分.jpg