Skip to content
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

Notifications cleanup #3

Merged
merged 4 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
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
27 changes: 16 additions & 11 deletions src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,29 @@ public Notification getNotification(String title, String text, String largeIcon,
}
}

public static void registerAllChannels(Context context) {
public static void registerAllChannels(Context context) {
// 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) {
String appName = ResourceResolver.newInstance(context).getString(("app_name"));
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
String backgroundServiceChannelName = ResourceResolver.newInstance(context).getString(("background_service_notification_channel_name"));
android.app.NotificationManager notificationManager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(createServiceChannel(appName));
notificationManager.createNotificationChannel(createSyncChannel());
notificationManager.createNotificationChannel(createAndroidPermissionsChannel(appName));
notificationManager.createNotificationChannel(createServiceChannel(backgroundServiceChannelName));
// comment out these lines as the main app is not using sync service and permissions channel
// TODO: consider dynamically register this notification channel based on config
// notificationManager.createNotificationChannel(createSyncChannel());

// String permissionChannelName = ResourceResolver.newInstance(context).getString(("android_permission_notification_channel_name"));
// notificationManager.createNotificationChannel(createAndroidPermissionsChannel(permissionChannelName));
}
}

public static void registerServiceChannel(Context context) {
// 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) {
String appName = ResourceResolver.newInstance(context).getString(("app_name"));
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
String backgroundServiceChannelName = ResourceResolver.newInstance(context).getString(("background_service_notification_channel_name"));
android.app.NotificationManager notificationManager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(createServiceChannel(appName));
notificationManager.createNotificationChannel(createServiceChannel(backgroundServiceChannelName));
}
}

Expand All @@ -120,6 +124,7 @@ public static void registerSyncChannel(Context context) {
public static NotificationChannel createServiceChannel(CharSequence name) {
NotificationChannel channel = new NotificationChannel(SERVICE_CHANNEL_ID, name, android.app.NotificationManager.IMPORTANCE_LOW);
channel.enableVibration(false);
channel.setShowBadge(false); // avoid badge count due to background service notification

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

return channel;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/marianhello/bgloc/sync/SyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public SyncAdapter(
batchManager = new BatchManager(this.getContext());
notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);

NotificationHelper.registerSyncChannel(context);
// comment out this line as the main app is not using sync service
// TODO: consider dynamically register this notification channel based on config
// NotificationHelper.registerSyncChannel(context);
}

/*
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">app_name</string>
<string name="background_service_notification_channel_name">Background location service</string>
</resources>