Search code examples
androidnotificationsandroid-notification-bar

How to set persistent notification background image


I have been developing an android application in which I need to set a persistent notification, which never goes away from the status bar. All things are setup properly, but I need to use full background in the notification without any text or anything. As I have attached a screenshot.

Notification Background like Yandex Search app

I have used the following code:

String notificationTitle = "Here My App Name Goes";
        String notificationMessage = "Search with ---------";

        Intent targetIntent = new Intent(Intent.ACTION_MAIN);
        targetIntent.setClassName(this, this.getClass().getName());

        int requestCode = AppSingleton.NOTIFICATION_ID_ALWAYS;

        PendingIntent contentIntent = PendingIntent.getActivity(this,
                requestCode, targetIntent, 0);
        String statusBarTickerText = "Search from ----------";
        int icon = R.drawable.ic_launcher;

        Notification notification = new Notification(icon,
                statusBarTickerText, System.currentTimeMillis());
        notification.flags = Notification.FLAG_ONGOING_EVENT
                | Notification.FLAG_NO_CLEAR;
        notification.setLatestEventInfo(this, notificationTitle,
                notificationMessage, contentIntent);

        nm.notify(AppSingleton.NOTIFICATION_ID_ALWAYS, notification);

I can set my app icon and write some text over it, but I need to set full background as it is the screenshot provided.

Please help me getting out of this prm.


Solution

  • I got the remedy for my problem, just have to make custom notification layout with some code change. Below is my edited code.:

    Notification notification = new Notification(
                    R.drawable.ic_launcher, "All ----",
                    System.currentTimeMillis());
    
            RemoteViews contentView = new RemoteViews(getPackageName(),
                    R.layout.notification);
    
            notification.contentView = contentView;
            final Intent notificationIntent = new Intent(this,
                    MainActivity.class);
    
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
            nm = (NotificationManager) getSystemService(MainActivity.NOTIFICATION_SERVICE);
    
            notification.flags |= Notification.FLAG_NO_CLEAR;
            notification.flags |= Notification.FLAG_ONGOING_EVENT;
    
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
            notification.contentIntent = contentIntent;
    
            nm.notify(AppSingleton.NOTIFICATION_ID_ALWAYS, notification);
    

    As you can see above RemoteViews class did my work without a problem. Just made a layout and referenced it here. And, boom got the background for my notification.