Remoteview is not applied in background

試著忘記壹切 提交于 2019-12-13 10:23:53

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/48781498/remoteview-is-not-applied-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!