app开发中,通知栏通知是比较常见的需求,这里有一个体验细节。弹出通知,用户下拉通知栏,点击后进入app中对应页面,看完后想返回上一页,于是点击了左上角或者右上角的返回按钮(一般来说应该都有),如果用户是从桌面点击通知进入app的,那么app栈内就没有对应的页面,返回上一页就不能通过常规的销毁当前页面来实现,这时就需要使用TaskStackBuilder了.
TaskStackBuilder,我的理解就是一个栈构建器,在xml里面给activity1配置一个parentActivityName=activity2,那么通过TaskStackBuilder启动activity1时,系统会先把activity2压入栈,然后再启动activity1,这时如果销毁activity1,activity2就会变为栈顶页面呈现在用户面前。这样就模拟出了历史页面效果,体验要比用户点击返回直接回到桌面要好.
public class NotificationUtil { /** * 展示通知 */ public static void showNotifiation(Context context, String title, String content, int nid, PushReceiverModel pushModel) { StaticMethod.debugEMSG("发送通知到通知栏"); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.push) .setContentTitle(title) .setContentText(content); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); Intent resultIntent; if (StaticVariable.rid != null && !StaticVariable.rid.equals("")) { if (StaticVariable.rid.equals("3")||StaticVariable.rid.equals("5")) { if (pushModel.getMessageType().equals(StaticConstant.系统通知)) { resultIntent = new Intent(context, XSJL_SysytemMsgActivity.class); stackBuilder.addParentStack(XSJL_SysytemMsgActivity.class); }else{ resultIntent = new Intent(context, XSJL_MsgBoxActivity.class); stackBuilder.addParentStack(XSJL_MsgBoxActivity.class); } } else if (StaticVariable.rid.equals("6")||StaticVariable.rid.equals("7")) { if (pushModel.getMessageType().equals(StaticConstant.微信对话)) { resultIntent = new Intent(context, WeChat_2_Activity.class); resultIntent.putExtra("cid", pushModel.getUid()); resultIntent.putExtra("title", pushModel.getName()); stackBuilder.addParentStack(WeChat_2_Activity.class); } else if (pushModel.getMessageType().equals(StaticConstant.销售新用户)) { resultIntent = new Intent(context, GJXQ_1_Activity.class); if (StaticMethod.isNotNull(pushModel.getUid())) { resultIntent.putExtra("cid", pushModel.getUid()); } else { resultIntent.putExtra("cid", pushModel.getAdvid()); } stackBuilder.addParentStack(GJXQ_1_Activity.class); } else if (pushModel.getMessageType().equals(StaticConstant.系统通知)) { resultIntent = new Intent(context, XSGW_SysytemMsgActivity.class); stackBuilder.addParentStack(XSGW_SysytemMsgActivity.class); } else if (pushModel.getMessageType().equals(StaticConstant.抢客户)) { resultIntent = new Intent(context, MainActivity.class); } else { resultIntent = new Intent(context, XSGW_TaskNotifyActivity.class); stackBuilder.addParentStack(XSGW_TaskNotifyActivity.class); } } else { resultIntent = new Intent(context, LaunchActivity.class); } } else { resultIntent = new Intent(context, LaunchActivity.class); } if (stackBuilder.getIntentCount() > 0) { stackBuilder.editIntentAt(0).putExtra("aid", StaticVariable.aid); stackBuilder.editIntentAt(0).putExtra("uid", StaticVariable.uid); } stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FLAG_ACTIVITY_NEW_TASK); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = mBuilder.build(); notification.flags = Notification.FLAG_AUTO_CANCEL; notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; long[] vibrate = {0, 100, 200, 300}; notification.vibrate = vibrate; mNotificationManager.notify(nid, notification); } }
里面有一个比较容易搞混的地方,就是parentActivity。这里用XSJL_SysytemMsgActivity来举例.
下面就是XSJL_SystemMsgActivity在AndroidManifest.xml里面的声明
<activity android:name=".page.base.xsjl.activity.XSJL_SysytemMsgActivity" android:parentActivityName=".page.base.xsjl.activity.MainActivity" />
parentActivityName指定的就是当XSJL_SystemMsgActivity被销毁时,需要显示的历史页面.
在代码里面这么写
resultIntent = new Intent(context, XSJL_SysytemMsgActivity.class); //resultIntent.putExtra("cid", pushModel.getUid());给XSJL_SysytemMsgActivity页面传递启动参数 //stackBuilder.editIntentAt(0).putExtra("aid", StaticVariable.aid);给parent页面传递启动参数, stackBuilder.addParentStack(XSJL_SysytemMsgActivity.class);
关键部分就这些,剩下的就是照着官网例子写的。
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FLAG_ACTIVITY_NEW_TASK); 获取到了PendingIntent,历史页面这一块就结束了,剩下的就是通知部分,没什么难点,抄代码改参数就ok了
时间: 2024-10-15 22:57:22