16
16
17
17
import android .os .PowerManager ;
18
18
import android .app .IntentService ;
19
+ import android .app .NotificationManager ;
19
20
import android .content .Intent ;
20
21
import android .support .v4 .app .NotificationCompat ;
21
22
import android .support .v4 .app .NotificationCompat .Builder ;
@@ -33,6 +34,7 @@ public class UploadService extends IntentService {
33
34
private static final String TAG = "AndroidUploadService" ;
34
35
35
36
private static final int UPLOAD_NOTIFICATION_ID = 1234 ; // Something unique
37
+ private static final int UPLOAD_NOTIFICATION_ID_DONE = 1235 ; // Something unique
36
38
private static final int BUFFER_SIZE = 4096 ;
37
39
private static final String NEW_LINE = "\r \n " ;
38
40
private static final String TWO_HYPHENS = "--" ;
@@ -56,6 +58,7 @@ public class UploadService extends IntentService {
56
58
public static final String SERVER_RESPONSE_CODE = "serverResponseCode" ;
57
59
public static final String SERVER_RESPONSE_MESSAGE = "serverResponseMessage" ;
58
60
61
+ private NotificationManager notificationManager ;
59
62
private Builder notification ;
60
63
private PowerManager .WakeLock wakeLock ;
61
64
private UploadNotificationConfig notificationConfig ;
@@ -98,8 +101,8 @@ public UploadService() {
98
101
@ Override
99
102
public void onCreate () {
100
103
super .onCreate ();
101
- notification = new NotificationCompat . Builder ( this )
102
- . setOngoing ( true );
104
+ notificationManager = ( NotificationManager ) getSystemService ( NOTIFICATION_SERVICE );
105
+ notification = new NotificationCompat . Builder ( this );
103
106
PowerManager pm = (PowerManager ) getSystemService (POWER_SERVICE );
104
107
wakeLock = pm .newWakeLock (PowerManager .PARTIAL_WAKE_LOCK , TAG );
105
108
}
@@ -360,35 +363,39 @@ private void createNotification() {
360
363
notification .setContentTitle (notificationConfig .getTitle ())
361
364
.setContentText (notificationConfig .getMessage ())
362
365
.setSmallIcon (notificationConfig .getIconResourceID ())
363
- .setProgress (100 , 0 , true );
366
+ .setProgress (100 , 0 , true )
367
+ .setOngoing (true );
364
368
startForeground (UPLOAD_NOTIFICATION_ID , notification .build ());
365
369
}
366
370
367
371
private void updateNotificationProgress (final int progress ) {
368
372
notification .setContentTitle (notificationConfig .getTitle ())
369
373
.setContentText (notificationConfig .getMessage ())
370
374
.setSmallIcon (notificationConfig .getIconResourceID ())
371
- .setProgress (100 , progress , false );
375
+ .setProgress (100 , progress , false )
376
+ .setOngoing (true );
372
377
startForeground (UPLOAD_NOTIFICATION_ID , notification .build ());
373
378
}
374
379
375
380
private void updateNotificationCompleted () {
381
+ stopForeground (notificationConfig .isAutoClearOnSuccess ());
376
382
if (!notificationConfig .isAutoClearOnSuccess ()) {
377
383
notification .setContentTitle (notificationConfig .getTitle ())
378
384
.setContentText (notificationConfig .getCompleted ())
379
385
.setSmallIcon (notificationConfig .getIconResourceID ())
380
- .setProgress (0 , 0 , false );
381
- startForeground (UPLOAD_NOTIFICATION_ID , notification .build ());
386
+ .setProgress (0 , 0 , false )
387
+ .setOngoing (false );
388
+ notificationManager .notify (UPLOAD_NOTIFICATION_ID_DONE , notification .build ());
382
389
}
383
- stopForeground (notificationConfig .isAutoClearOnSuccess ());
384
390
}
385
391
386
392
private void updateNotificationError () {
393
+ stopForeground (false );
387
394
notification .setContentTitle (notificationConfig .getTitle ())
388
395
.setContentText (notificationConfig .getError ())
389
396
.setSmallIcon (notificationConfig .getIconResourceID ())
390
- .setProgress (0 , 0 , false );
391
- startForeground ( UPLOAD_NOTIFICATION_ID , notification . build () );
392
- stopForeground ( false );
397
+ .setProgress (0 , 0 , false )
398
+ . setOngoing ( false );
399
+ notificationManager . notify ( UPLOAD_NOTIFICATION_ID_DONE , notification . build () );
393
400
}
394
401
}
0 commit comments