Note that there are some explanatory texts on larger screens.

plurals
  1. PONotifications disappear as soon as service ends
    text
    copied!<p>I've got a service that scrapes a website for data then if necessary gives the user a notification.</p> <p>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.</p> <pre><code>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); } </code></pre> <p>If it makes any difference (pretty sure it doesn't) i'm using the application context here.</p> <p>My services is started periodically, processes a website, notifies if needed and then closes.</p> <p>At the end of my service instead of just calling stopSelf() i sleep for 30 or so seconds and then call stopSelf(). Then the notification stays for 30 seconds and then disappears. Nothing relevant appears in logcat at the time the notifications disappear.</p> <p>I've only been able to test using ICS so far.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload