首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果在其上触摸,通知将消失

如果在其上触摸,通知将消失
EN

Stack Overflow用户
提问于 2020-01-08 17:45:08
回答 3查看 57关注 0票数 0

我正在构建一个通知,其中有接受和拒绝按钮,但同时我也想打开活动时,用户触摸通知。但在我的例子中,只有两个按钮都有效,当触摸到按钮以外的其他部分时,它就会消失。

我的努力:

代码语言:javascript
复制
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());

我也尝试过查看其他线程,但没有任何帮助。任何帮助都将受到感谢,并提前表示感谢。

EN

回答 3

Stack Overflow用户

发布于 2020-01-08 18:21:54

变化

代码语言:javascript
复制
setAutoCancel(true)

代码语言:javascript
复制
setAutoCancel(false)
票数 0
EN

Stack Overflow用户

发布于 2020-01-08 20:22:07

只需尝试添加:

代码语言:javascript
复制
  PendingIntent contentIntent = builder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                        | PendingIntent.FLAG_ONE_SHOT);
票数 0
EN

Stack Overflow用户

发布于 2020-01-13 01:10:31

我正在为未来的新用户回答我自己的问题。

为什么通知在触摸时消失(在2个按钮之外,但仍在通知中)

因为基础挂起意图不是一种活动,而是一种服务。您可以注意到上面的代码。它应该是这样的:

代码语言:javascript
复制
    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);

两个按钮的代码和它们的挂起意图,也就是服务是可以的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59643191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档