我正在使用以下代码:
private void startForeground(boolean isActive) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(isActive ? R.string.title_agent_active : R.string.title_agent_inactive))
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
}
和
@Override
protected void onHandleIntent(Intent intent) {
startForeground(true);
...
使用此代码通知不会出现。
但是如果我用 NotificationManager.notify(...) 替换 startForeground(...) — 它会显示通知,所以我认为至少通知构建代码是可以的。
问题出在哪里?
最佳答案
如果您使用的是 IntentService
,则服务将在 onHandleIntent()
方法完成执行后立即销毁,然后通知将被删除。
关于android - 通知不会出现在前台服务中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35034037/