diff options
author | mvglasow <michael@vonglasow.com> | 2019-02-26 18:17:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-26 18:17:07 +0100 |
commit | 5352c402c7aeef45206d99577217f20a283fb8b1 (patch) | |
tree | 08c5ec0b7f48f61b384d989d40c8096303899a43 | |
parent | bb34c13a582e1a6b13f3da7a342fc7eccb081b42 (diff) | |
parent | 35635c0cb3766350ee6d5957747a18b507bb4f00 (diff) | |
download | navit-5352c402c7aeef45206d99577217f20a283fb8b1.tar.gz |
Merge pull request #756 from mvglasow/android-notification
Fix:port/Android:Create notification channel on API 26+
-rw-r--r-- | navit/android/res/values/strings.xml | 1 | ||||
-rw-r--r-- | navit/android/src/org/navitproject/navit/Navit.java | 50 |
2 files changed, 44 insertions, 7 deletions
diff --git a/navit/android/res/values/strings.xml b/navit/android/res/values/strings.xml index ae80265e3..f321991e7 100644 --- a/navit/android/res/values/strings.xml +++ b/navit/android/res/values/strings.xml @@ -7,6 +7,7 @@ <string name="cancel">Cancel</string> <!-- NOTIFICATION --> + <string name="channel_name">Navit</string> <string name="notification_ticker">Navit started</string> <string name="notification_event_default">Navit running</string> diff --git a/navit/android/src/org/navitproject/navit/Navit.java b/navit/android/src/org/navitproject/navit/Navit.java index 5c2d15a72..bc2af1632 100644 --- a/navit/android/src/org/navitproject/navit/Navit.java +++ b/navit/android/src/org/navitproject/navit/Navit.java @@ -26,6 +26,7 @@ import android.app.AlertDialog; import android.app.Application; import android.app.Dialog; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; @@ -90,6 +91,7 @@ public class Navit extends Activity { private static final int NavitSelectStorage_id = 43; private static String NavitLanguage; private static Resources NavitResources = null; + private static final String CHANNEL_ID = "org.navitproject.navit"; private static final String NAVIT_PACKAGE_NAME = "org.navitproject.navit"; private static final String TAG = "Navit"; static String map_filename_path = null; @@ -118,6 +120,25 @@ public class Navit extends Activity { } } + private void createNotificationChannel() { + /* + * Create the NotificationChannel, but only on API 26+ because + * the NotificationChannel class is new and not in the support library + */ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + CharSequence name = getString(R.string.channel_name); + //String description = getString(R.string.channel_description); + int importance = NotificationManager.IMPORTANCE_LOW; + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); + //channel.setDescription(description); + /* + * Register the channel with the system; you can't change the importance + * or other notification behaviors after this + */ + NotificationManager notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + } public void removeFileIfExists(String source) { File file = new File(source); @@ -343,16 +364,31 @@ public class Navit extends Activity { // NOTIFICATION // Setup the status bar notification // This notification is removed in the exit() function + if (isLaunch) + createNotificationChannel(); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Grab a handle to the NotificationManager PendingIntent appIntent = PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0); - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - builder.setContentIntent(appIntent); - builder.setAutoCancel(false).setOngoing(true); - builder.setContentTitle(getTstring(R.string.app_name)); - builder.setContentText(getTstring(R.string.notification_event_default)); - builder.setSmallIcon(R.drawable.ic_notify); - Notification NavitNotification = builder.build(); + Notification NavitNotification; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Notification.Builder builder; + builder = new Notification.Builder(getApplicationContext(), CHANNEL_ID); + builder.setContentIntent(appIntent); + builder.setAutoCancel(false).setOngoing(true); + builder.setContentTitle(getTstring(R.string.app_name)); + builder.setContentText(getTstring(R.string.notification_event_default)); + builder.setSmallIcon(R.drawable.ic_notify); + NavitNotification = builder.build(); + } else { + NotificationCompat.Builder builder; + builder = new NotificationCompat.Builder(getApplicationContext()); + builder.setContentIntent(appIntent); + builder.setAutoCancel(false).setOngoing(true); + builder.setContentTitle(getTstring(R.string.app_name)); + builder.setContentText(getTstring(R.string.notification_event_default)); + builder.setSmallIcon(R.drawable.ic_notify); + NavitNotification = builder.build(); + } nm.notify(R.string.app_name, NavitNotification);// Show the notification if ((ContextCompat.checkSelfPermission(this, |