Added notification support for Android 8 (#389)

* Added notification support for Android 8

Channel ID for notification

* Update ForegroundService.java

* Delete ForegroundService.java

* Update ForegroundService.java

* SetImportance

Add setImportance when SDK >= 26

* SetImportance

rm SetImportance

* Update README.md

* Revert "SetImportance"

This reverts commit 9535246bf3c32bb57f91f61ea82698600ff94f31.

* Revert "SetImportance"

This reverts commit 6f5e5e8bc3fc2dff7d0abcea901df12d8516f800.

* Revert "Update README.md"

This reverts commit 65c69077ab948193147627319a6266c765822831.

* Update README.md

* Revert "Update README.md"

This reverts commit 9cbe65e7da02c36b0de7039597f5b9a472edd19e.
This commit is contained in:
Marco Sirini 2019-01-31 18:26:29 +01:00 committed by Sebastián Katzer
parent a2352f5d8b
commit b5950b578d

View File

@ -1,6 +1,5 @@
/* /*
Copyright 2013-2017 appPlant GmbH Copyright 2013-2017 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file or more contributor license agreements. See the NOTICE file
distributed with this work for additional information distributed with this work for additional information
@ -8,9 +7,7 @@
to you under the Apache License, Version 2.0 (the to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@ -33,6 +30,7 @@ import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.app.NotificationChannel;
import org.json.JSONObject; import org.json.JSONObject;
@ -65,6 +63,7 @@ public class ForegroundService extends Service {
// Partial wake lock to prevent the app from going to sleep when locked // Partial wake lock to prevent the app from going to sleep when locked
private PowerManager.WakeLock wakeLock; private PowerManager.WakeLock wakeLock;
private final String CHANNEL_ID = "cordova-plugin-background-mode-id";
/** /**
* Allow clients to call on to the service. * Allow clients to call on to the service.
*/ */
@ -153,6 +152,22 @@ public class ForegroundService extends Service {
* @param settings The config settings * @param settings The config settings
*/ */
private Notification makeNotification(JSONObject settings) { private Notification makeNotification(JSONObject settings) {
// use channelid for Oreo and higher
if(Build.VERSION.SDK_INT >= 26){
// The user-visible name of the channel.
CharSequence name = "cordova-plugin-background-mode";
// The user-visible description of the channel.
String description = "cordova-plugin-background-moden notification";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(this.CHANNEL_ID, name,importance);
// Configure the notification channel.
mChannel.setDescription(description);
getNotificationManager().createNotificationChannel(mChannel);
}
String title = settings.optString("title", NOTIFICATION_TITLE); String title = settings.optString("title", NOTIFICATION_TITLE);
String text = settings.optString("text", NOTIFICATION_TEXT); String text = settings.optString("text", NOTIFICATION_TEXT);
boolean bigText = settings.optBoolean("bigText", false); boolean bigText = settings.optBoolean("bigText", false);
@ -168,6 +183,10 @@ public class ForegroundService extends Service {
.setOngoing(true) .setOngoing(true)
.setSmallIcon(getIconResId(settings)); .setSmallIcon(getIconResId(settings));
if(Build.VERSION.SDK_INT >= 26){
notification.setChannelId(this.CHANNEL_ID);
}
if (settings.optBoolean("hidden", true)) { if (settings.optBoolean("hidden", true)) {
notification.setPriority(Notification.PRIORITY_MIN); notification.setPriority(Notification.PRIORITY_MIN);
} }
@ -207,6 +226,7 @@ public class ForegroundService extends Service {
Notification notification = makeNotification(settings); Notification notification = makeNotification(settings);
getNotificationManager().notify(NOTIFICATION_ID, notification); getNotificationManager().notify(NOTIFICATION_ID, notification);
} }
/** /**