Java 如果触摸到它,通知就会消失
我正在构建一个通知,其中包含接受和拒绝按钮,但同时我还希望在用户触摸通知时打开活动。但在我的例子中,只有两个按钮都起作用,当触摸到按钮以外的其他部分时,它就消失了 我的努力:Java 如果触摸到它,通知就会消失,java,android,notifications,Java,Android,Notifications,我正在构建一个通知,其中包含接受和拒绝按钮,但同时我还希望在用户触摸通知时打开活动。但在我的例子中,只有两个按钮都起作用,当触摸到按钮以外的其他部分时,它就消失了 我的努力: Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); String NOTIFICATION_CHANNEL_ID = "BizPhone Channel"; int id = (int) Sys
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String NOTIFICATION_CHANNEL_ID = "BizPhone Channel";
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(context, incoming_service.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
intent.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "BizPhone Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("BizPhone related notifications");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
// assuming your main activity
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setCategory(Notification.CATEGORY_CALL)
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_call_black_24dp)
.setPriority(Notification.FLAG_INSISTENT)
.setContentTitle(phoneNumber)
.setContentText("Incoming Call")
.setSound(alarmSound)
.setContentIntent(pendingIntent)
.setContentInfo("Call");
Intent accept = new Intent(context, incoming_service.class);
accept.setAction(incoming_service.ANSWER);
accept.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
accept.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
accept.putExtra(Intent.EXTRA_MIME_TYPES, callType);
accept.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntentYes = PendingIntent.getService(context, 10, accept, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_permission_granted_24dp, "ACCEPT", pendingIntentYes);
Intent decline = new Intent(context, incoming_service.class);
decline.setAction(incoming_service.REJECT);
decline.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
decline.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
decline.putExtra(Intent.EXTRA_MIME_TYPES, callType);
decline.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntentNo = PendingIntent.getService(context, 11, decline, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_call_end_black_24dp, "REJECT", pendingIntentNo);
//notificationBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notificationBuilder.build());
我也尝试过查看其他线程,但没有帮助。如有任何帮助,将不胜感激。更改
setAutoCancel(true)
到
请尝试添加:
PendingIntent contentIntent = builder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
我为未来的用户回答我自己的问题 为什么触摸时通知消失(2个按钮之外,但仍处于通知状态) 因为基地吊挂不是一项活动,而是一项服务。您可以注意到上面的代码。应该是这样的:
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, CallActivity.class);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
还有两个按钮的代码和他们的悬挂物,也就是服务还可以 尝试删除。setAutoCancel(true)并重试。它已停止消失,但仍处于接触状态。它会隐藏并且不会打开活动,因为它是标志,CATEGORY_CALL它必须在触摸时打开Activity on touch on not clickedIt通知,即使按钮未点击编辑已停止消失,但仍在触摸时它会隐藏并不会打开Activity,因为它是标志性的,CATEGORY_CALL必须在触控通知时打开activity,即使未单击按钮。我们为GetActivity之类的活动或getService之类的服务或getBroadcast之类的广播创建挂起内容,但不使用生成器。。请删除帖子,否则其他人将投否决票
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, CallActivity.class);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);