Question

In my Android app, I provide a notification both when a user begins uploading a file and when the file upload is complete. When the upload begins, the message "Uploading" appears in the Navigation Bar. I want the message "Uploading complete" to flash in the Navigation Bar when the upload is complete, but the message never appears in the Navigation Bar. When you expand the Notification drawer, though, the correct message is shown.

How do I get the content title to flash in the navigation bar?

NotificationManager mNotificationManager =
                        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                Constant.currentlyUploading.decrementAndGet();
                Constant.totalUploadedImages.incrementAndGet();
                if(Constant.currentlyUploading.get()>0){
                    String files_count;
                    if(Constant.currentlyUploading.get() ==1){
                        files_count = "file";
                    }else{
                        files_count = "files";
                    }
                    Builder uploadBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle("Uploading")
                    .setContentText(Constant.currentlyUploading.get()+ " " + files_count +  " uploading");
                    Notification uploadNotif = uploadBuilder.build();
                    mNotificationManager.notify(Constant.NOTIFICATION_UPLOADING_ID, uploadNotif);
                }else{
                    // cancel uploading notification
                    CancelNotification(this.context, Constant.NOTIFICATION_UPLOADING_ID);

                    String uploaded_count;
                    if(Constant.totalUploadedImages.get()>1){
                        uploaded_count = "files";
                    }else{
                        uploaded_count = "file";
                    }
                    Builder uploadedBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle("Upload complete")
                    .setContentText(Constant.totalUploadedImages.get()+" " + uploaded_count + " uploaded")
                    .setAutoCancel(true);
                    Notification uploadNotif = uploadedBuilder.build();
                    mNotificationManager.notify(Constant.NOTIFICATION_UPLOADED_ID, uploadNotif);    
                    Constant.totalUploadedImages.getAndSet(0);      

                }
                //Removing one from the step's image upload counter
                uploadStep.decTotalUploading();
                if(stepAdapter!=null){
                    stepAdapter.notifyDataSetChanged();
                }

                //Removing the path from the list of currently uploading images
                uploadStep.removeCurrentlyUploading(filepath);

            }

and my Cancel Notification method:

public static void CancelNotification(Context ctx, int notifyId){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancel(notifyId);
}
Was it helpful?

Solution

The answer ended up being quite simple; just had to add the following line to my updated notification:

.setTicker("Upload Complete")

Here's the complete code:

Builder uploadedBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("Upload complete")
                .setTicker("Upload Complete")
                .setContentText(Constant.totalUploadedImages.get()+" " + uploaded_count + " uploaded")
                .setAutoCancel(true);
                Notification uploadNotif = uploadedBuilder.build();
                mNotificationManager.notify(Constant.NOTIFICATION_UPLOADING_ID, uploadNotif);   
                Constant.totalUploadedImages.getAndSet(0);      

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top