简体   繁体   中英

Notification disappearing if touched over it

I am building a notification which has ACCEPT and REJECT buttons but at the same time i also wants to open the activity when user touches over the notification. But in my case only both buttons working and when touched over the other parts than buttons it just disappears.

My efforts :

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

I have tried by looking at other threads too but it did not help. Any help would be appreciated and thanks in advance.

Change

setAutoCancel(true)

to

setAutoCancel(false)

Just try to add :

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

I am answering my own question for future incoming users.

Why notification was disappearing on touch ( outside of 2 buttons but still on notification )

Because the base pendingintent was not a activity and it was a service. You can notice the code above. It should have been like :

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

And the 2 buttons code and their pendingintent which is service was okay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM