前台服务中的静默通知

Dig*_*son 5 service notifications android

我有多个应用程序可以一起工作以完成相同的工作,并且它们都属于同一个开发人员.每个应用程序在后台运行长时间运行的服务,并继续处理用户的输入.问题是这些服务很长时间不能在后台运行,因为Android系统会杀死它们.所以我想使用前台服务而不是后台服务来防止系统杀死它们.但是,我不想在通知抽屉中使用多个不同的通知来骚扰用户.

我发现在没有在Android O中分配频道的情况下创建通知将让系统启动前台服务而不显示通知.类似于以下内容:

Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(......);
builder.setTicker(......);
builder.setContentText(......);
builder.setSmallIcon(......);
builder.setLargeIcon(......);
Notification notification = builder.build();
startForeground(111, notification);
Run Code Online (Sandbox Code Playgroud)

所以我想通过一个应用程序创建一个通道来显示通知,并创建一个没有其他应用程序的通道的通知,如前所述.在这种情况下,用户将看到我的所有应用程序的一个通知.

这个解决方案适合我.但我想知道它是否是前台服务中无意中使用的通知.我担心谷歌会暂停我的应用程序!

你们知道是否可以实施该解决方案?或者有没有办法将通知组合在一个组中,即使它们是不同的应用程序?

我的目标只是让通知不那么烦恼用户.此外,我了解JobScheduler和JobIntentService解决方案.但他们不会在我的情况下完成工作,因为我想保持服务运行.不喜欢做一份工作并停止服务......

小智 3

您可以使用IMPORTANCE_LOWhttps://developer.android.com/training/notify-user/channels#importance)创建通知通道。应该没有声音。

或者您也可以使用setOnlyAlertOnce()https://developer.android.com/training/notify-user/build-notification#Updating)并且声音只会一次。