我正在使用以下代码:
$id = $this->route('gecko')
和
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);
}
此代码通知不会出现。
但是如果我用NotificationManager.notify(...)替换startForeground(...) - 它会显示通知,所以,我认为,至少通知构建代码是可以的。
哪里可能是问题?
答案 0 :(得分:5)
如果您使用IntentService
,只要onHandleIntent()
方法完成执行,服务就会被销毁,然后通知将被删除。
答案 1 :(得分:1)
onCreate()
方法noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Service
类尝试此解决方案(适用于我的应用):
private void fg() {
Intent intent = launchIntent(this);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent);
noti = mBuilder.build();
noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
startForeground(_.ID, noti);
}