MaxSmile / EasyVPN-Free

Open source Android application on base of OpenVPN and VpnGate projects
GNU General Public License v3.0
297 stars 153 forks source link

Android 8.1 or 9.0 Error when clicking on connect #19

Open hamzology opened 5 years ago

hamzology commented 5 years ago

When I have compiled an apk and tried to run on android 8.0 it was working fine but when I installed the same apk in my android 9.0 and then click on connect vpn button it is crashing and giving the below error:

Fatal Exception: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x14a color=0x00000000 category=service actions=2 vis=PRIVATE) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2082) at android.os.Handler.dispatchMessage(Handler.java:109) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:7539) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

i-build-web-apps commented 5 years ago

In OpenVPNService.java, add the following method:

@RequiresApi(api = Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId, String channelName){
    NotificationChannel chan = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
    chan.setLightColor(Color.BLUE) ;
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    service.createNotificationChannel(chan);
    return channelId ;
}

Then replace:

    android.app.Notification.Builder nbuilder = new Notification.Builder(this);

with:

    String channelID = "" ;
    android.app.Notification.Builder nbuilder ;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channelID = createNotificationChannel("my_service", "My Background Service") ;
        nbuilder = new Notification.Builder(this, channelID);
    }
    else
        nbuilder = new Notification.Builder(this);