我在代码中没有进行任何更改,突然我的IntentService(以ForegroundService开头)根本没有发出任何通知。它仍在运行,但没有任何通知。
我尝试更改频道ID,如果android阻止了通知,则查找了该应用,但自上次以来没有任何更改。
在我启动服务的片段中:
public void startRefresh() {
Intent fetchIntent = new Intent(getActivity(), FetchService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getActivity().startForegroundService(fetchIntent);
} else {
getActivity().startService(fetchIntent);
}
}
服务本身:
public class FetchService extends IntentService {
public static final String notificationChannelId = "DEFAULT_CHANNEL";
public FetchService() {
super("FetchService");
}
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
startForeground(1, getProgressNotification("fetching started"));
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopForeground(true);
stopSelf();
}
private Notification getProgressNotification(String text) {
return new NotificationCompat.Builder(this, notificationChannelId)
.setSmallIcon(R.drawable.ic_round_refresh_24px_blk)
.setContentTitle(getString(R.string.fetching_new_data))
.setContentText(text)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setProgress(0, 0, true).build();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(FetchService.notificationChannelId, name, importance);
channel.setDescription(description)
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
我希望前台服务像往常一样显示通知。此时服务正在运行(调用onHandleIntent())并正确停止。通知根本不显示。
编辑: 因此,在重新安装该应用程序后,它似乎可以再次工作...奇怪,但我希望它可以继续工作