解雇后,单挑通知在全屏模式下停留

时间:2018-04-18 16:21:56

标签: android android-notifications

我看到一些关于正在进行的heads-up notifications的奇怪行为,这些行为是由全屏意图支持的。给定两个活动,MainActivity只包含一个显示通知的按钮:

public class MainActivity extends AppCompatActivity {
    private static final String CHANNEL  = "TestChannel";
    public static final int NOTIFICATION_ID = 1;
    private static final int REQUEST_CODE = 2;

    private NotificationManager notificationManager;

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

        this.notificationManager = getSystemService(NotificationManager.class);
        Button showNotification = findViewById(R.id.show_notification);

        ensureNotificationChannelCreated();
        showNotification.setOnClickListener(v -> notificationManager.notify(NOTIFICATION_ID, createNotification()));
    }

    @TargetApi(O) private void ensureNotificationChannelCreated() {
        if (SDK_INT >= O) {
            NotificationChannel incoming = new NotificationChannel(CHANNEL, "Channel Name", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(incoming);
        }
    }

    public Notification createNotification() {
        Intent intent = new Intent(this, ContentActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return new NotificationCompat.Builder(this, CHANNEL)
                .setContentText("Test notification")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pi)
                .setFullScreenIntent(pi, true)
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .build();
    }
}

全屏意图提供的活动ContentActivity

public class ContentActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);

        NotificationManager notificationManager = getSystemService(NotificationManager.class);    
        notificationManager.cancel(NOTIFICATION_ID);
    }
}
  • 如果我点击该按钮,然后点击通知,我会看到ContentActivity并且可以一遍又一遍地重复此过程。
  • 但是,如果我向上滑动以隐藏抬头通知,请点击下拉托盘中的通知,以后我点击系统会立即显示showNotification按钮ContentActivity,并且不显示抬头通知

我希望在随后的按钮点击时仍然会显示抬头通知,即使先前已向上刷过通知。我还缺少其他标志或设置吗?

1 个答案:

答案 0 :(得分:1)

Android用于在单挑通知中选择全屏活动的过程似乎有些不透明。 From the documentation(我的大胆):

  

意图启动而不是将通知发布到状态栏。

     

系统用户界面可以选择显示抬头通知,而不是在用户使用设备时启动此意图。

似乎没有关于如何做出这个决定的任何文件。我发现的唯一解决方法是确保将每个通知发布到具有不同ID的经理。所以在MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    Random random = new Random(System.currentTimeMillis());
    showNotification.setOnClickListener(v -> {
         int notificationId = Math.abs(random.nextInt());
         notificationManager.notify(notificationId, createNotification(notificationId))
    });
}

... 

public Notification createNotification() {
    Intent intent = new Intent(this, ContentActivity.class);
    intent.putExtra("NOTIFICATION_ID", notificationId);
    PendingIntent pi = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ...
}

ContentActivity

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

    int notificationId = getIntent().getIntExtra("NOTIFICATION_ID", -1);
    if (notificationId >= 0) {
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.cancel(notificationId);
    }
}

不理想,但似乎每次都保证提醒通知,这就是我想要的行为。