1818
1919import org .renpy .android .Hardware ;
2020
21+ //imports for channel definition
22+ import android .app .NotificationManager ;
23+ import android .app .NotificationChannel ;
24+ import android .graphics .Color ;
2125
2226public class PythonService extends Service implements Runnable {
2327
@@ -90,13 +94,13 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9094 protected void doStartForeground (Bundle extras ) {
9195 String serviceTitle = extras .getString ("serviceTitle" );
9296 String serviceDescription = extras .getString ("serviceDescription" );
93-
9497 Notification notification ;
9598 Context context = getApplicationContext ();
9699 Intent contextIntent = new Intent (context , PythonActivity .class );
97100 PendingIntent pIntent = PendingIntent .getActivity (context , 0 , contextIntent ,
98101 PendingIntent .FLAG_UPDATE_CURRENT );
99- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .HONEYCOMB ) {
102+
103+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O ) {
100104 notification = new Notification (
101105 context .getApplicationInfo ().icon , serviceTitle , System .currentTimeMillis ());
102106 try {
@@ -109,7 +113,19 @@ protected void doStartForeground(Bundle extras) {
109113 IllegalArgumentException | InvocationTargetException e ) {
110114 }
111115 } else {
112- Notification .Builder builder = new Notification .Builder (context );
116+ // for android 8+ we need to create our own channel
117+ // https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1
118+ String NOTIFICATION_CHANNEL_ID = "org.kivy.p4a" ; //TODO: make this configurable
119+ String channelName = "PythonSerice" ; //TODO: make this configurable
120+ NotificationChannel chan = new NotificationChannel (NOTIFICATION_CHANNEL_ID , channelName ,
121+ NotificationManager .IMPORTANCE_NONE );
122+
123+ chan .setLightColor (Color .BLUE );
124+ chan .setLockscreenVisibility (Notification .VISIBILITY_PRIVATE );
125+ NotificationManager manager = (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
126+ manager .createNotificationChannel (chan );
127+
128+ Notification .Builder builder = new Notification .Builder (context , NOTIFICATION_CHANNEL_ID );
113129 builder .setContentTitle (serviceTitle );
114130 builder .setContentText (serviceDescription );
115131 builder .setContentIntent (pIntent );
0 commit comments