Reputation: 1889
I have an app that is using services to download images, but I wish to notify the user when the downloads are complete even when the user is outside the app(a notification in the status bar) also that works in minSdkVersion="8", I know there are some answers about this but I don't get the notification works: here are some of my code (it works only the notification is the problem):
public class saveJsonContact1Service extends IntentService {
private static FileOutputStream fos;
private int result = Activity.RESULT_CANCELED;
public saveJsonContact1Service() {
super("saveJsonContact1Service");
}
@Override
protected void onHandleIntent(Intent intent) {
downloadImages();//To download images
sendNotification();//To send a notification after the donwloadImages() method has ended
}
private void sendNotification(){
CharSequence sns = "content";
CharSequence sns2 = "content";
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
notification.setLatestEventInfo(this, sns, sns2, pendingIntent);
notificationManager.notify(0, notification);
}
}
How can I modify the sendNotification()?? Thanks
Upvotes: 0
Views: 585
Reputation: 6179
Try this:
private void sendNotification(){
CharSequence title = "The title";
CharSequence message = "My message";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(title);
mBuilder.setContentText(message);
mBuilder.setTicker(message);
mBuilder.setWhen(System.currentTimeMillis());
NotificationManager notificationManager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
mBuilder.setContentIntent(pendingIntent);
notificationManager.notify(0, mBuilder.build());
}
Upvotes: 3