开发者社区> 问答> 正文

无法清除通知并从通知中打开片段

我正在尝试清除通知。但是它仍然在那里。无法从通知中打开片段活动。下面是我的代码,

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int sticky;

        try {
            AndroidLogger.log(5, TAG, "Missed call notification on start");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
                NotificationChannel channel = new NotificationChannel("1", "Call Notification", importance);
                channel.setDescription("Get alert for missed call");
                channel.setShowBadge(true);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
            Intent notifyIntent = new Intent(this, ViewFragment.class);
            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


            PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
                    .setSmallIcon(R.drawable.nexge_logo)
                    .setContentTitle("Missed Call")
                    .setContentText(intent.getStringExtra("Number"))
                    .setContentIntent(pIntent)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_MAX);

            Notification buildNotification = mBuilder.build();
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //mNotifyMgr.notify(1, buildNotification);

            startForeground(1,buildNotification);
        } catch (Exception exception) {
            AndroidLogger.error(1, TAG, "Exception while starting service", exception);
        }
        return START_NOT_STICKY;
    }
}

有人帮我解决这个问题。提前致谢。以下是我没有得到正确答案的另一个问题。帮助我解决 关于android中未接来电的通知

展开
收起
Puppet 2020-01-08 17:29:42 513 0
1 条回答
写回答
取消 提交回答
  • 我对类似问题的解决方案是将Android文档中的PendingIntent标志更改为PendingIntent.FLAG_ONE_SHOT:

    指示此PendingIntent只能使用一次的标志。与getActivity(Context,int,Intent,int),getBroadcast(Context,int,Intent,int)和getService(Context,int,Intent,int)一起使用。

    如果设置,则在调用send()之后,它将自动为您取消,并且以后尝试通过它发送的任何尝试都会失败。

    并添加通知FLAG_AUTO_CANCEL标志:

    
    mBuilder.flags |= Notification.FLAG_AUTO_CANCEL;
    
    

    这应该确保通知一旦使用就会被删除。

    编辑:应致电

    Notification notification = mBuilder.build();
    

    首先,然后

    notification.flags = Notification.FLAG_AUTO_CANCEL;
    
    2020-01-08 17:30:26
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载