0

我有一个通知,我想在创建后立即显示。

我现在的代码:

Intent myIntent = new Intent(WearRunActivity.this, ResultsActivity.class);
    myIntent.putExtra("distance", distance);
    myIntent.putExtra("time", chronometer.getText());
    myIntent.putExtra("pace", pace);
    myIntent.putExtra("speed", speedAc);
    myIntent.putExtra("lSteps", leftSteps);
    myIntent.putExtra("rSteps", rightSteps);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    //WearRunActivity.this.startActivityIfNeeded(myIntent, 1);


    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.card_icon)
                    .setContentTitle("REPORT")
                    .setContentText(chronometer.getText())
                    .setLargeIcon(BitmapFactory.decodeResource(
                            getResources(), R.drawable.card_icon))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);

    notification_manager = NotificationManagerCompat.from(this);

    notification_manager.notify(1, notificationBuilder.build());

使用此代码,通知已创建,但不会自动显示在屏幕上。我需要浏览所有待处理的通知来搜索它。我想要的是这个通知在创建后自动显示在屏幕上,有什么建议吗?

4

1 回答 1

0

我找到了办法!
在构建通知时添加行.setOngoing(True)以便在发送通知时,它将位于所有待处理通知的顶部并显示在屏幕上。

NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.card_icon)
                    .setContentTitle("Report")
                    .setContentText(chronometer.getText())
                    .setLargeIcon(BitmapFactory.decodeResource(
                            getResources(), R.drawable.card_background_blur))
                    .setAutoCancel(true)
                    .setOngoing(true)
                    .setContentIntent(pendingIntent);
于 2015-10-01T11:48:43.220 回答