有没有一种方法来保持通知活动状态,直到用户点击清楚了吗? [英] Is there a way to keep notification active until user clicks clear?

查看:282
本文介绍了有没有一种方法来保持通知活动状态,直到用户点击清楚了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在后台运行,并通知用户,如果他们有一个新的PDF查看服务。当他们点击该通知的窗户打开让他们下载PDF(S)的选项。问题是,一旦用户关闭这个窗口,他们没有办法回到那个画面,因为当他们点击它通知被清除。我想这个通知保持有效,直到用户点击清除通知事件,他们不小心关闭了通知打开的窗口中。*

I have a service that runs in the background and notifies the user if they have a new PDF to view. When they click the notification a windows opens up giving them the option to download the PDF(s). The thing is that once the user closes this window they have no way to get back to that screen since the notification was cleared when they clicked it. I want this notification to remain active until the user clicks 'Clear Notifications' in the event they accidentally close the window the notification opens.*

*我不创建应用程序永久访问的窗口/视图的原因是因为该通知是时间敏感。 20分钟后的PDF文件供他们随时查看。创建另一个永久看法是多余的。此通知只是在那里允许用户早一点查看PDF文件。

*The reason I don't create a permanently accessible window/view in the app is because the notifications are time sensitive. After 20 minutes the PDFs are available for them to view at any time. Creating another permanent view would be redundant. This notification is only there to allow the user to view the PDFs a little earlier.

编辑:继@Rahul古普塔提供的答案,我想通了,我的应用程序是使用'FLAG_AUTO_CANCEL如果我没有指定一个标志。所以,我的解决方案,我只是把另一个标志。由于我使用的Appcelerator的钛,我没有setAutoCancel()函数,因此,这不是我的选择。

following the answer provided by @Rahul Gupta, I figured out that my application was using 'FLAG_AUTO_CANCEL' if I did not specify a flag. So for my solution I just put in another flag. Since I'm using Titanium Appcelerator, I don't have a setAutoCancel() function so that wasn't an option for me.

推荐答案

有关API低于11,你可以设置Notification.FLAG_NO_CLEAR。这可以这样实现的:

For API below 11, you can set Notification.FLAG_NO_CLEAR. This can be implemented like this:

// 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支持库的时候,我们可以实现它是这样的:

For API levels above 11, or when using the Android Support Library, one can implement it like this:

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类。

or you can use NotificationCompat class.

public NotificationCompat.Builder setAutoCancel (boolean autoCancel)

设置该标志将使它所以当用户点击它在面板的通知将自动取消。当通知取消了与setDeleteIntent(的PendingIntent)设置的PendingIntent将播出。

Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. The PendingIntent set with setDeleteIntent(PendingIntent) will be broadcast when the notification is canceled.

这篇关于有没有一种方法来保持通知活动状态,直到用户点击清楚了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆