From b5950b578d7a670903987be6798c62cb3b49f51b Mon Sep 17 00:00:00 2001 From: Marco Sirini Date: Thu, 31 Jan 2019 18:26:29 +0100 Subject: [PATCH] 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. --- src/android/ForegroundService.java | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index e313082..544b4fb 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -1,6 +1,5 @@ /* Copyright 2013-2017 appPlant GmbH - Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information @@ -8,9 +7,7 @@ to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -33,6 +30,7 @@ import android.os.Binder; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; +import android.app.NotificationChannel; 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 private PowerManager.WakeLock wakeLock; + private final String CHANNEL_ID = "cordova-plugin-background-mode-id"; /** * Allow clients to call on to the service. */ @@ -153,6 +152,22 @@ public class ForegroundService extends Service { * @param settings The config 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 text = settings.optString("text", NOTIFICATION_TEXT); boolean bigText = settings.optBoolean("bigText", false); @@ -168,6 +183,10 @@ public class ForegroundService extends Service { .setOngoing(true) .setSmallIcon(getIconResId(settings)); + if(Build.VERSION.SDK_INT >= 26){ + notification.setChannelId(this.CHANNEL_ID); + } + if (settings.optBoolean("hidden", true)) { notification.setPriority(Notification.PRIORITY_MIN); } @@ -207,6 +226,7 @@ public class ForegroundService extends Service { Notification notification = makeNotification(settings); getNotificationManager().notify(NOTIFICATION_ID, notification); + } /**