1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Add notification channel to all notifications

Android SDK 26 requirs setting a channel in order to show nofiticaitons
This commit is contained in:
tal 2018-08-18 11:47:28 +03:00 committed by Tom Hacohen
parent 2cb804cfc5
commit 20feee0df7
2 changed files with 22 additions and 1 deletions

View File

@ -1,11 +1,14 @@
package com.etesync.syncadapter; package com.etesync.syncadapter;
import android.app.Activity; import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
@ -21,6 +24,8 @@ import at.bitfire.ical4android.CalendarStorageException;
import at.bitfire.vcard4android.ContactsStorageException; import at.bitfire.vcard4android.ContactsStorageException;
public class NotificationHelper { public class NotificationHelper {
private static final String CHANNEL_ID = "EteSync_default";
final NotificationManagerCompat notificationManager; final NotificationManagerCompat notificationManager;
final Context context; final Context context;
final String notificationTag; final String notificationTag;
@ -77,10 +82,10 @@ public class NotificationHelper {
} }
public void notify(String title, String content, String bigText, Intent intent) { public void notify(String title, String content, String bigText, Intent intent) {
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
int icon; int icon;
String category; String category;
String tag;
//Check if error was configured //Check if error was configured
if (throwable == null) { if (throwable == null) {
icon = R.drawable.ic_sync_dark; icon = R.drawable.ic_sync_dark;
@ -93,6 +98,7 @@ public class NotificationHelper {
builder.setLargeIcon(App.getLauncherBitmap(context)) builder.setLargeIcon(App.getLauncherBitmap(context))
.setContentTitle(title) .setContentTitle(title)
.setContentText(content) .setContentText(content)
.setChannelId(CHANNEL_ID)
.setAutoCancel(true) .setAutoCancel(true)
.setCategory(category) .setCategory(category)
.setSmallIcon(icon) .setSmallIcon(icon)
@ -140,4 +146,15 @@ public class NotificationHelper {
finish(); finish();
} }
} }
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.notification_channel_name);
NotificationChannel channel =
new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
} }

View File

@ -339,4 +339,8 @@
<!-- Event (from Etar) --> <!-- Event (from Etar) -->
<string name="event_info_organizer">Organizer:</string> <string name="event_info_organizer">Organizer:</string>
<!-- Notifications -->
<string name="notification_channel_name">EteSync</string>
<string name="notification_channel_description">EteSync notifications</string>
</resources> </resources>