【问题标题】:Notification disappearing if touched over it如果触摸它,通知就会消失
【发布时间】:2020-01-08 09:45:08
【问题描述】:

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

我的努力:

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) 并重试。
  • 它停止消失,但仍然在触摸它隐藏并且不打开活动,因为它是 FLAG_INSISTENT、CATEGORY_CALL 它必须打开触摸通知的活动,即使没有点击按钮

标签: java android notifications


【解决方案1】:

改变

setAutoCancel(true)

setAutoCancel(false)

【讨论】:

  • 它停止消失,但仍然在触摸它隐藏并且不打开活动,因为它是 FLAG_INSISTENT、CATEGORY_CALL 它必须打开触摸通知的活动,即使没有点击按钮
【解决方案2】:

尝试添加:

  PendingIntent contentIntent = builder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                        | PendingIntent.FLAG_ONE_SHOT);

【讨论】:

  • 我们为 getActicty 之类的活动或 getService 之类的服务或 getBroadcast 之类的广播而不使用 BUILDER 创建 PendingIntent。
【解决方案3】:

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

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

因为基本的pendingintent 不是一个活动,而是一个服务。你可以注意到上面的代码。应该是这样的:

    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);

并且 2 个按钮的代码和它们作为服务的pendingintent 没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多