简体   繁体   中英

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:

private void showNotification() {

    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)  
            .setWhen(System.currentTimeMillis()) 
            .setContentTitle("Uploading")
            .setContentText("Your upload is in progress.")
            .setOngoing(true)
            .build();

    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked . What am I missing?

Deployment target is API level 24, so a missing NotificationChannel should not be the cause.

try it :

    PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
    android.app.Notification pp = new NotificationCompat.Builder(getmContext())
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentText(getMessage())
            .setContentIntent(pendingIntent)
            .build();

    NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, pp);

This is my code which is working fine with target SDK 25

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);

        Notification notification;
        notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)

                .setStyle(inboxStyle)

                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                .setContentText(message)
                .build();

        NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

where NOTIFICATION_ID

 public static final int NOTIFICATION_ID = 100;

I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.

Changed the icon colour in the xml from

android:fillColor="#FF000000" to android:fillColor="#FFFFFFFF"

and it works like a charm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM