I'm implementing a background downloading using a notification with button that suggest user to download a content. on click of the button it will trigger a Download Manager to download the link provided and when download is complete, a new notification should show with the same content but the button text is changed as well as it's function. Now what happen is that on complete download a log is fired but the notification doesn't show. Here's my code inside the IntentService
Register and unregister the receiver:
@Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
//Define the receiver
downloadReceiver = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Log.e("Download", "Download complete!");
createNotification("Download complete!", "Click here to view the content.");
}
}
};
registerReceiver(downloadReceiver, new IntentFilter(Commons.downloadManager.ACTION_DOWNLOAD_COMPLETE));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(downloadReceiver);
}
The method for showing the notification:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void createNotification(String title, String message) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setSummaryText(message);
notiStyle.bigPicture(remote_picture);
Intent splash = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, splash, PendingIntent.FLAG_UPDATE_CURRENT);
// build notification
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setColor(getResources().getColor(android.R.color.black))
.setContentText(message)
.setSound(alarmSound)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_LIGHTS)
.setStyle(notiStyle);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
NOTIFICATION_ID+=1;
}
The remote_picture
is a Bitmap from the first notification which is dismissed after I started downloading the content. Also downloadManager
is static from Commons
class.
Note that the Log.e("Download", "Download complete!");
fires on complete but no notification appears. How can I show the notification on complete download?