在我的Android应用中,我会在用户开始上传文件和文件上传完成时提供通知。上传开始时,导航栏中会显示“正在上传”消息。上传完成后,我希望导航栏中的“上传完成”消息闪烁,但消息永远不会出现在导航栏中。但是,展开“通知”抽屉时,会显示正确的消息。
如何在导航栏中将内容标题设为Flash?
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);
}
和我的取消通知方法:
public static void CancelNotification(Context ctx, int notifyId){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
答案 0 :(得分:0)
答案最终很简单;只需将以下行添加到我更新的通知中:
.setTicker("Upload Complete")
这是完整的代码:
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);
}