API 26以下的自定义通知行为差异

问题描述

正在处理自定义通知,我需要以折叠/展开形式显示通知。 我发现在api 26及以下版本的设备上接收到的通知总是以扩展形式出现。

即使我在构建器中设置了setCustomContentView()

Same在api 28中工作正常。

为了更好地理解这里的问题,提到了文章link,我们可以在其中克隆代码并针对不同的api级别对其进行测试。

这里也提到了代码,以便快速响应。

public class MainActivity extends AppCompatActivity {

    private String NOTIFICATION_TITLE = "Notification Sample App";
    private String CONTENT_TEXT = "Expand me to see a detailed message!";

    EditText mEditText;
    Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEditText = (EditText) findViewById(R.id.edit_text);

        mButton = (Button) findViewById(R.id.button);
        mButton.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendNotification();
            }
        });
    }

    private void sendNotification() {

        RemoteViews collapsedView = new RemoteViews(getPackageName(),R.layout.view_collapsed_notification);
        collapsedView.setTextViewText(R.id.timestamp,DateUtils.formatDateTime(this,System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME));

        RemoteViews expandedView = new RemoteViews(getPackageName(),R.layout.view_expanded_notification);
        expandedView.setTextViewText(R.id.timestamp,DateUtils.FORMAT_SHOW_TIME));
        expandedView.setTextViewText(R.id.notification_message,mEditText.getText());
        // adding action to left button
        Intent leftIntent = new Intent(this,NotificationIntentService.class);
        leftIntent.setAction("left");
        expandedView.setonClickPendingIntent(R.id.left_button,PendingIntent.getService(this,leftIntent,PendingIntent.FLAG_UPDATE_CURRENT));
        // adding action to right button
        Intent rightIntent = new Intent(this,NotificationIntentService.class);
        rightIntent.setAction("right");
        expandedView.setonClickPendingIntent(R.id.right_button,1,rightIntent,PendingIntent.FLAG_UPDATE_CURRENT));


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // these are the three things a NotificationCompat.Builder object requires at a minimum
                .setSmallIcon(R.drawable.ic_pawprint)
                .setContentTitle(NOTIFICATION_TITLE)
                .setContentText(CONTENT_TEXT)
                // notification will be dismissed when tapped
                .setAutoCancel(true)
                // tapping notification will open MainActivity
                .setContentIntent(PendingIntent.getActivity(this,new Intent(this,MainActivity.class),0))
                // setting the custom collapsed and expanded views
                .setCustomContentView(collapsedView)
                //.setCustomHeadsUpContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                // setting style to DecoratedCustomViewStyle() is necessary for custom views to display
                .setStyle(new android.support.v7.app.NotificationCompat.DecoratedCustomViewStyle());

        // retrieves android.app.notificationmanager
        notificationmanager notificationmanager = (android.app.notificationmanager) getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(0,builder.build());
    }
}

您可以找到完整的代码here

如果这是预期的行为,或者我缺少某些东西,请告诉我。

在Api 24,26和28上进行了测试。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)