i am using an app i wrote on android 8 with no issues. this app uses a backgroud service to read GPS coordinates, process them and do some logic. i'm trying to move it to android 10 and changed gradle settings accordingly, but i'm getting a crash (testing it on the device galaxy s20) right after calling "startForeground" - this is the service onCreate Code:
public class ScanService extends Service {
@Override
public void onCreate() {
List<UserLocationManager> locationsManagers = GetLocationsManagers();
_locListener = new MyLocationListener(locationsManagers);
_locManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new Notification.Builder(this, "lsxChannel")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Location Trigger")
.setContentText("Scanning Location...")
.setContentIntent(pendingIntent).build();
startForegroundService(notificationIntent);
startForeground(1337, notification); // after this one the app crashes
}
i have the permission in the manifest:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
if i remark the "startForeground(1337, notification);" line - the app doesn't crash so i'm sure this is the line that causes the problem.
the logcat output during the crash is:
2020-11-15 17:27:06.190 17455-17455/com.tandenkore.locationtrigger D/AndroidRuntime: Shutting down VM
2020-11-15 17:27:06.192 17455-17455/com.tandenkore.locationtrigger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tandenkore.locationtrigger, PID: 17455
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2188)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
2020-11-15 17:27:06.195 6449-8919/? W/ActivityManager: crash : com.tandenkore.locationtrigger,0
2020-11-15 17:27:06.199 17455-17455/com.tandenkore.locationtrigger I/Process: Sending signal. PID: 17455 SIG: 9
2020-11-15 17:27:06.218 6449-6785/? W/InputDispatcher: channel '83d46a2 com.tandenkore.locationtrigger/com.tandenkore.locationtrigger.Activities.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9, fd=599
2020-11-15 17:27:06.218 6449-6785/? E/InputDispatcher: channel '83d46a2 com.tandenkore.locationtrigger/com.tandenkore.locationtrigger.Activities.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
ok - found the problem - it seems android 10 needs notification channel to be created - reference from https://ask.xiaolee.net/questions/1065268
working now.