Search code examples
androidandroid-remoteview

Remoteview is not applied in background


I've applied Remoteview to notification. It works when user is in foreground, but when user is in background, notification is not showed as remoteview.

How can I apply remoteview when user is background?

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.doggy_downgrade3)
            .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
            .setContentTitle("Doggy")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setNumber(1)
            .setPriority(Notification.PRIORITY_MAX)
            .setContent(remoteViews)
            .setContentIntent(pendingIntent);

The above is my code for building a notification.


Solution

  • You need to set two different views, one is custom content view and the other is Big content view and set it as follows -

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(icon)
                .setCustomContentView(contentViewSmall)
                .setCustomBigContentView(contentViewBig)
                .setContentTitle("Custom Notification")
                .setContentIntent(contentIntent)
                .setAutoCancel(true)
                .setWhen(when);
    
        mNotificationManager.notify(1, notificationBuilder.build());
    

    Also read my blog on Custom Layouts for Push notifications