Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on android 8.1 #416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
Expand All @@ -38,6 +39,7 @@ Licensed to the Apache Software Foundation (ASF) under one

import static android.os.PowerManager.PARTIAL_WAKE_LOCK;

import android.graphics.Color;
/**
* Puts the service in a foreground state, where the system considers it to be
* something the user is actively aware of and thus not a candidate for killing
Expand Down Expand Up @@ -167,6 +169,23 @@ private Notification makeNotification(JSONObject settings) {
.setContentText(text)
.setOngoing(true)
.setSmallIcon(getIconResId(settings));
//upgrade to sdk 26, fix problems with android 8.1
if (android.os.Build.VERSION.SDK_INT >= 26) {
//Set the channel’s ID//
String CHANNEL_ONE_ID = "com.yourapp.ONE";
//Set the channel’s user-visible name//
String CHANNEL_ONE_NAME = "Channel One";
//This only needs to be run on Devices on Android O and above
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.setLightColor(Color.MAGENTA);
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
notification.setChannelId(CHANNEL_ONE_ID);
}

if (settings.optBoolean("hidden", true)) {
notification.setPriority(Notification.PRIORITY_MIN);
Expand Down