服务结束后通知立即消失

8

我有一个服务,可以从网站上获取数据,如果需要会向用户发送通知。

问题是一旦服务关闭(可能立即关闭),通知就会消失。以下是应用程序所有通知代码。我没有放置任何取消通知的代码。

public static void CreateNotificationCompat(int notificationID, String link, String title, String text, Context ctx)
{
    Intent notificationIntent = new Intent("android.intent.action.VIEW", Uri.parse(link));
    PendingIntent contentIntent = PendingIntent.getActivity(ctx.getApplicationContext(), 0, notificationIntent, 0);

    NotificationManager nm = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    Resources res = ctx.getResources();

    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.notify_icon)
                .setWhen(System.currentTimeMillis())
                .setContentTitle(title)
                .setContentText(text)
                .setAutoCancel(false);

    Notification n = builder.getNotification();

    nm.notify(notificationID, n);
}

如果有任何区别的话(我很确定没有),我在这里使用应用程序上下文。
我的服务定期启动,处理网站,如果需要则通知,然后关闭。
在我的服务结束时,我不仅会调用stopSelf(),还会休眠30秒左右,然后再调用stopSelf()。然后通知会保持30秒,然后消失。在通知消失时,logcat中没有任何相关信息。
到目前为止,我只能使用ICS进行测试。

这应该没问题。通常情况下,只有在您的应用程序被强制关闭时,通知才会消失。我认为可能是某些代码崩溃了。看看你用来“休眠”的代码(你是否真的告诉线程要休眠?)以及运行该代码的线程将会很有趣。为了实验的缘故,尝试移除30秒的休眠并查看是否发生相同的行为也会很有趣。 - kabuko
请问有关于取消通知的代码在哪里?另外,您应该避免直接使用字符串如android.intent.action.*,因为这是Intent类中可用的常量。@user1531605 - JoxTraex
如果在发布通知之前,即在同一代码执行链上请求停止服务,则通知将被清除。 - S.D.
4个回答

4

您发布的代码是有效的。我在jb上从活动和服务两方面进行了测试,当组件停止时通知仍然存在。我甚至验证过我可以杀死进程而通知仍然存在。尝试简化应用程序并查看问题是否仍然存在。特别检查以确保您没有错误地调用了通知的取消操作。


2

我已经阅读了它,但那不是我想要的。通知应该能够通过“清除通知”按钮进行清除,因此FLAG_NO_CLEAR无法帮助。而且这不是一个持续性事件。从文档中可以看出:“如果此通知是针对发生在特定时间点上的某事的引用(例如未接来电),则不应设置它。” 这正是这个通知所指的,是关于发生在特定时间点上的某件事的引用。虽然作为旁注,我测试了一下这些标志,结果仍然相同。当服务关闭时,通知仍然消失了。 - user1531605
你在4.x之前的安卓系统上测试过你的代码吗?模拟器就可以了。 - Marcin Orlowski
请发布您的服务源代码,只包括相关的部分,例如创建和销毁。 - JoxTraex

1
请在同一设备上尝试旧的方法,并告诉我们它是否表现相同:
private static void CreateNotificationCompat(int notificationID, String link, String title, String text, Context ctx) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
    Notification notification = new Notification(R.drawable.notify_icon, title, System.currentTimeMillis());
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    PendingIntent contentIntent;
    Bundle bundle = new Bundle();
    Intent notificationIntent = new Intent("android.intent.action.VIEW", Uri.parse(link));
    contentIntent = PendingIntent.getActivity(ctx, notificationID,
                notificationIntent, 0);
    notification.setLatestEventInfo(ctx, title, text,
            contentIntent);
    mNotificationManager.notify(notificationID, notification);
}

1
只是一个提示,当使用startForeground()然后调用stopSelf()时会出现这种行为。

我有一个类似的问题。我需要使用startForeground和stopSelf,但在stopSelf之后,我想保留通知。在运行时,它不能被解除,但在stopSelf之后,我希望用户能够清除它/解除它或重新触摸我的通知上的按钮以重新启动服务。目前,由于我找不到其他方法,我计划发布另一个通知,上面写着“点击此处重新开始”。你觉得呢? - frankish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接