Skip to content

Support android API 27 #359

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

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
23 changes: 23 additions & 0 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
package de.appplant.cordova.plugin.background;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -33,9 +34,12 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.graphics.Color;

import org.json.JSONObject;

import java.lang.reflect.Method;

import static android.os.PowerManager.PARTIAL_WAKE_LOCK;

/**
Expand Down Expand Up @@ -168,6 +172,12 @@ private Notification makeNotification(JSONObject settings) {
.setOngoing(true)
.setSmallIcon(getIconResId(settings));

String channelId = "";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
channelId = createNotificationChannel();
notification.setChannelId(channelId);
}

if (settings.optBoolean("hidden", true)) {
notification.setPriority(Notification.PRIORITY_MIN);
}
Expand All @@ -192,6 +202,19 @@ private Notification makeNotification(JSONObject settings) {
return notification.build();
}

@TargetApi(Build.VERSION_CODES.O)
private String createNotificationChannel(){
String channelId = "background_mode_service";
String channelName = "Background mode service";
NotificationChannel chan = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
chan.setLightColor(Color.BLUE);
chan.setImportance(NotificationManager.IMPORTANCE_NONE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
return channelId;
}

/**
* Update the notification.
*
Expand Down