【问题标题】:Is there a way to keep notification active until user clicks clear?有没有办法在用户点击清除之前保持通知处于活动状态?
【发布时间】:2013-11-18 18:04:07
【问题描述】:

我有一个在后台运行的服务,如果他们有新的 PDF 文件要查看,它会通知用户。当他们单击通知时,会打开一个窗口,让他们可以选择下载 PDF。问题是,一旦用户关闭此窗口,他们就无法返回该屏幕,因为当他们单击通知时通知已被清除。如果用户不小心关闭了通知打开的窗口,我希望此通知保持活动状态,直到用户单击“清除通知”。*

*我没有在应用程序中创建永久可访问的窗口/视图的原因是通知是时间敏感的。 20 分钟后,PDF 可供他们随时查看。创建另一个永久视图将是多余的。此通知仅允许用户提前查看 PDF。

编辑:按照@Rahul Gupta 提供的答案,如果我没有指定标志,我发现我的应用程序正在使用“FLAG_AUTO_CANCEL”。所以对于我的解决方案,我只是放了另一个标志。因为我使用的是 Titanium Appcelerator,所以我没有 setAutoCancel() 函数,所以这不是我的选择。

【问题讨论】:

    标签: android notifications window


    【解决方案1】:

    对于 11 以下的 API,您可以设置 Notification.FLAG_NO_CLEAR。这可以这样实现:

    // Create notification
    Notification note = new Notification(R.drawable.your_icon, "Example ", System.currentTimeMillis());
    
    // Set notification message
    note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);
    
    // THIS LINE IS THE IMPORTANT ONE            
    // This notification will not be cleared by swiping or by pressing "Clear all"
    note.flags |= Notification.FLAG_NO_CLEAR;
    

    对于 11 以上的 API 级别,或者使用 Android 支持库时,可以这样实现:

    Notification noti = new Notification.Builder(mContext)
        .setContentTitle("title")
        .setContentText("content")
        .setSmallIcon(R.drawable.yourIcon)
        .setLargeIcon(R.drawable.yourBigIcon)
        .setOngoing(true) // Again, THIS is the important line. This method lets the notification to stay.
        .build();
    

    或者你可以使用 NotificationCompat 类。

    public NotificationCompat.Builder setAutoCancel (boolean autoCancel)
    

    设置此标志将使通知在用户在面板中单击时自动取消。用setDeleteIntent(PendingIntent)设置的PendingIntent会在通知取消时广播。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多