好久没写文章,今天遇到一个新需求,app正在通话过程切换到后台,点击通知栏的时候 回到通话界面。
直接上代码吧
首先是实现通知的代码
NotificationCompat.Builder notificationBuild =null;
int NOTIFY_ID = 100;
NotificationManager mNotificationManager;
boolean isHasNotification = false;
android.app.Notification notification;
/**
* 显示通知栏
*/
public void showNotification(String title,String content){
if (mNotificationManager == null&&!isHasNotification){
Intent deleteIntent = new Intent(this, NotificationTipsReceiver.class);
notificationBuild = new NotificationCompat.Builder(this);
notificationBuild.setAutoCancel(false);//设置能手动取消
notificationBuild.setOngoing(true);//设置不能取消
notificationBuild.setContentTitle(title);
notificationBuild.setSmallIcon(R.mipmap.icon);//设置图标
notificationBuild.setTicker("小尾巴 正在接单中");
notificationBuild.setDefaults(android.app.Notification.DEFAULT_ALL);
notificationBuild.setPriority(android.app.Notification.PRIORITY_MAX);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
0, deleteIntent, 0);
notificationBuild.setContentIntent(pendingIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
notification = notificationBuild.build();
mNotificationManager.notify(NOTIFY_ID, notification);
isHasNotification = true;
}
下面是通知点击处理类
public class NotificationTipsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//判断app进程是否存活
boolean isApplive = SystemUtil.isAppAlive(context, "com.iol8.te.tr");
Intent dialogIntent = new Intent(context, DialogueActivity.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);//注意这里是关键 经过测试Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED也可以用Intent.FLAG_ACTIVITY_SINGLE_TOP 也可以做到 具体区别没有去深入测试
if (isApplive) {
if (WYIMManager.isDialogue) {
if (!SystemUtil.isAppRunningForeground(context)){//app在后台运行
context.startActivity(dialogIntent);
}
return;
}
}
}
}
另外附上一个Intent的flag 启动介绍(感谢这位大神的分享)
Android Intent flag 介绍