我看到了一些关于正在进行的总目通知的奇怪行为,这些行为被全屏的意图所支持。给定两个活动,只包含一个按钮以显示通知的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的任何时候都会立即显示,并且提示通知不会出现。我希望提醒通知仍然显示在随后的按钮单击,即使通知之前已被滑动向上。是否有其他标志或设置是我遗漏的?
发布于 2018-04-19 10:09:19
Android用于选择全屏活动而不是提示通知的过程似乎有点不透明。从文件中 (我的粗体):
启动而不是将通知发送到状态栏的意图。 系统UI 可以选择来显示提醒通知,而不是在用户使用设备时启动这个意图。
关于这一决定是如何作出的,似乎没有任何文件。我找到的唯一解决办法是确保每个通知都以不同的ID发送给经理。
@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);
}
}不太理想,但似乎每次都会发出提醒通知,这就是我想要的行为。
https://stackoverflow.com/questions/49904553
复制相似问题