0

I'm using Foreground Services for notification and it's working on other mobile phones as well it's working on an emulator also but in MI phone it's not working it shows error like this.

2020-07-01 17:47:25.783 4338-4338/com.example.notification_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.notification_app, PID: 4338
java.lang.RuntimeException: Unable to create service com.example.notification_app.my_service: java.lang.SecurityException: Permission Denial: startForeground from pid=4338, uid=10175 requires android.permission.FOREGROUND_SERVICE
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3577)
    at android.app.ActivityThread.access$1400(ActivityThread.java:200)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6810)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
 Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=4338, uid=10175 requires android.permission.FOREGROUND_SERVICE
    at android.os.Parcel.createException(Parcel.java:1953)
    at android.os.Parcel.readException(Parcel.java:1921)
    at android.os.Parcel.readException(Parcel.java:1871)
    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5104)
    at android.app.Service.startForeground(Service.java:695)
    at com.example.notification_app.my_service.startMyOwnForeground(my_service.java:57)
    at com.example.notification_app.my_service.onCreate(my_service.java:44)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)
    at android.app.ActivityThread.access$1400(ActivityThread.java:200) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:201) 
    at android.app.ActivityThread.main(ActivityThread.java:6810) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

2020-07-01 17:47:25.826 4338-4338/? I/Process: Sending signal. PID: 4338 SIG: 9

and in line number 48, I called a function startForeground(1, new Notification());

1 Answers1

2

If you read carefully in the end of second line

requires android.permission.FOREGROUND_SERVICE

I was also facing the same error in one of my project I resolve it by adding permission in the manifest file. So just simply add foreground permission in the manifest file.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>

Hope it's work for you.