简体   繁体   English

一旦服务结束,通知就会消失

[英]Notifications disappear as soon as service ends

I've got a service that scrapes a website for data then if necessary gives the user a notification. 我有一个服务,可以在网站上搜索数据,然后在必要时向用户发送通知。

The problem I'm having is that the notifications disappear as soon as the service closes (which could be instantly). 我遇到的问题是,一旦服务关闭(可能是即时的),通知就会消失。 The below code is all of the notification code the app has. 以下代码是应用程序的所有通知代码。 I haven't put in any code to cancel the notifications. 我没有输入任何代码来取消通知。

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

If it makes any difference (pretty sure it doesn't) i'm using the application context here. 如果它有任何区别(很确定它没有)我在这里使用应用程序上下文。

My services is started periodically, processes a website, notifies if needed and then closes. 我的服务定期启动,处理网站,如果需要通知然后关闭。

At the end of my service instead of just calling stopSelf() i sleep for 30 or so seconds and then call stopSelf(). 在我的服务结束时,而不是只是调用stopSelf(),我睡了30秒左右,然后调用stopSelf()。 Then the notification stays for 30 seconds and then disappears. 然后通知停留30秒然后消失。 Nothing relevant appears in logcat at the time the notifications disappear. 在通知消失时,logcat中没有任何相关内容。

I've only been able to test using ICS so far. 到目前为止,我只能使用ICS进行测试。

The code you have posted works. 您发布的代码有效。 I tested it from both an activity and a service on jb, and the notification stays when the components are stopped. 我在jb上的活动和服务上测试了它,并且当组件停止时通知保持不变。 I even verified that I could kill the process and the notification stays. 我甚至证实我可以杀死进程并且通知仍然存在。 Try to strip down the app and see if the problem persists. 尝试删除应用程序,看看问题是否仍然存在。 Specifically check to see that you don't call cancel on the notification by mistake. 具体检查是否有错误地通知取消通知取消。

You seem to forgot check developer manual first. 您似乎忘记了首先查看开发人员手册。 See here http://developer.android.com/guide/topics/ui/notifiers/notifications.html and look for FLAG_ONGOING_EVENT and FLAG_NO_CLEAR flags. 请参阅http://developer.android.com/guide/topics/ui/notifiers/notifications.html并查找FLAG_ONGOING_EVENT和FLAG_NO_CLEAR标记。

I've only been able to test using ICS so far. 到目前为止,我只能使用ICS进行测试。

That's why simulators are quite useful. 这就是模拟器非常有用的原因。 Try it. 试试吧。 It's part of SDK. 它是SDK的一部分。

Please try the old way on the same device and tell us if it behaves the same way: 请在同一台设备上尝试旧方法并告诉我们它是否表现相同:

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

只是注意当使用startForeground()然后调用stopSelf()时会发生此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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