Skip to content

Commit b204fad

Browse files
Merge pull request #12471 from Isira-Seneviratne/Fix-notifications
Fix foreground service issues
2 parents d17eae9 + 08f51ab commit b204fad

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,23 @@
9696
android:exported="false"
9797
android:label="@string/title_activity_about" />
9898

99-
<service android:name=".local.subscription.services.SubscriptionsImportService" />
100-
<service android:name=".local.subscription.services.SubscriptionsExportService" />
101-
<service android:name=".local.feed.service.FeedLoadService"
99+
<service
100+
android:name=".local.subscription.services.SubscriptionsImportService"
102101
android:foregroundServiceType="dataSync" />
103102

103+
<service
104+
android:name=".local.subscription.services.SubscriptionsExportService"
105+
android:foregroundServiceType="dataSync" />
106+
107+
<service
108+
android:name=".local.feed.service.FeedLoadService"
109+
android:foregroundServiceType="dataSync" />
110+
111+
<service
112+
android:name="androidx.work.impl.foreground.SystemForegroundService"
113+
android:foregroundServiceType="dataSync"
114+
tools:node="merge" />
115+
104116
<activity
105117
android:name=".PanicResponderActivity"
106118
android:exported="true"
@@ -425,6 +437,7 @@
425437
</activity>
426438
<service
427439
android:name=".RouterActivity$FetcherService"
440+
android:foregroundServiceType="dataSync"
428441
android:exported="false" />
429442

430443
<!-- opting out of sending metrics to Google in Android System WebView -->

app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationWorker.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.schabi.newpipe.local.feed.notifications
22

33
import android.content.Context
4+
import android.content.pm.ServiceInfo
5+
import android.os.Build
46
import android.util.Log
57
import androidx.core.app.NotificationCompat
68
import androidx.work.Constraints
@@ -83,7 +85,9 @@ class NotificationWorker(
8385
.setPriority(NotificationCompat.PRIORITY_LOW)
8486
.setContentTitle(applicationContext.getString(R.string.feed_notification_loading))
8587
.build()
86-
setForegroundAsync(ForegroundInfo(FeedLoadService.NOTIFICATION_ID, notification))
88+
// ServiceInfo constants are not used below Android Q, so 0 is set here
89+
val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0
90+
setForegroundAsync(ForegroundInfo(FeedLoadService.NOTIFICATION_ID, notification, serviceType))
8791
}
8892

8993
companion object {

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,17 @@ public boolean shouldUpdateBufferingSlot() {
167167
&& notificationBuilder.mActions.get(2).actionIntent != null);
168168
}
169169

170-
171170
public void createNotificationAndStartForeground() {
172171
if (notificationBuilder == null) {
173172
notificationBuilder = createNotification();
174173
}
175174
updateNotification();
176175

177-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
178-
player.getService().startForeground(NOTIFICATION_ID, notificationBuilder.build(),
179-
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
180-
} else {
181-
player.getService().startForeground(NOTIFICATION_ID, notificationBuilder.build());
182-
}
176+
// ServiceInfo constants are not used below Android Q, so 0 is set here
177+
final int serviceType = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
178+
? ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK : 0;
179+
ServiceCompat.startForeground(player.getService(), NOTIFICATION_ID,
180+
notificationBuilder.build(), serviceType);
183181
}
184182

185183
public void cancelNotificationAndStopForeground() {

0 commit comments

Comments
 (0)