Skip to content

Commit

Permalink
notif: Use Zulip's custom notification sound on Android
Browse files Browse the repository at this point in the history
Fixes: zulip#340
  • Loading branch information
rajveermalviya committed Oct 4, 2024
1 parent 3668025 commit a7ffb60
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
Binary file added android/app/src/main/res/raw/chime2.m4a
Binary file not shown.
Binary file added android/app/src/main/res/raw/chime3.m4a
Binary file not shown.
Binary file added android/app/src/main/res/raw/chime4.m4a
Binary file not shown.
2 changes: 1 addition & 1 deletion android/app/src/main/res/raw/keep.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
https://github.com/zulip/zulip-flutter/issues/528
-->
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@drawable/zulip_notification"
tools:keep="@drawable/zulip_notification,@raw/chime2,@raw/chime3,@raw/chime4"
/>
95 changes: 93 additions & 2 deletions lib/notifications/display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@ import '../widgets/theme.dart';

AndroidNotificationHostApi get _androidHost => ZulipBinding.instance.androidNotificationHost;

enum NotificationSound {
chime2(resourceName: 'chime2', fileDisplayName: 'Zulip - Low Chime.m4a'),
chime3(resourceName: 'chime3', fileDisplayName: 'Zulip - Chime.m4a'),
chime4(resourceName: 'chime4', fileDisplayName: 'Zulip - High Chime.m4a');

const NotificationSound({
required this.resourceName,
required this.fileDisplayName});

final String resourceName;
final String fileDisplayName;
}

/// Service for configuring our Android "notification channel".
class NotificationChannelManager {
/// The channel ID we use for our one notification channel, which we use for
/// all notifications.
// TODO(?) at launch make sure this channel-id doesn't collide with the one
// present in zulip-mobile.
// Previous values: 'messages-1'
@visibleForTesting
static const kChannelId = 'messages-1';
static const kChannelId = 'messages-2';

@visibleForTesting
static const kDefaultNotificationSound = NotificationSound.chime3;

/// The vibration pattern we set for notifications.
// We try to set a vibration pattern that, with the phone in one's pocket,
Expand All @@ -39,6 +56,78 @@ class NotificationChannelManager {
@visibleForTesting
static final kVibrationPattern = Int64List.fromList([0, 125, 100, 450]);

/// Prepare our notification sounds; return a URL for our default sound.
///
/// Where possible, this copies each of our notification sounds into shared storage
/// so that the user can choose between them in the system notification settings.
///
/// Returns a URL for our default notification sound: either in shared storage
/// if we successfully copied it there, or else as our internal resource file.
static Future<String> _ensureInitNotificationSounds() async {
String defaultSoundUrl = await _androidHost.getRawResourceUrlFromName(
kDefaultNotificationSound.resourceName);

final shouldUseResourceFile = switch (await ZulipBinding.instance.deviceInfo) {
// Before Android 10 Q, we don't attempt to put the sounds in shared media storage.
// Just use the resource file directly.
// TODO(android-sdk-29): Simplify this away.
AndroidDeviceInfo(:var sdkInt) => sdkInt <= 28,
_ => true,
};
if (shouldUseResourceFile) return defaultSoundUrl;

final soundsTodo = NotificationSound.values.toList();

// First, look to see what notification sounds we've already stored,
// and check against our list of sounds we have.

final storedSounds = await _androidHost.listStoredNotificationSounds();
for (final storedSound in storedSounds) {
assert(storedSound != null); // TODO(#942)

// If the file is one we put there, and has the name we give to our
// default sound, then use it as the default sound.
if (storedSound!.fileName == kDefaultNotificationSound.fileDisplayName
&& storedSound.isOwner) {
defaultSoundUrl = storedSound.uri;
}

// If it has the name of any of our sounds, then don't try to add
// that sound. This applies even if we didn't put it there: the
// name is taken, so if we tried adding it anyway it'd get some
// other name (like "Zulip - Chime (1).m4a", with " (1)" added).
// Which means the *next* launch would try to add it again ad infinitum.
// We could avoid this given some other way to uniquely identify the
// file, but haven't found an obvious one.
//
// This does mean it's possible the file isn't the one we would have
// put there... but it probably is, just from a debug vs. release build
// of the app (because those have different package names). And anyway,
// this is a file we're supplying for the user in case they want it, not
// something where the app depends on it having specific content.
soundsTodo.removeWhere((v) => v.fileDisplayName == storedSound.fileName);
}

// If that leaves any sounds we haven't yet put into shared storage
// (e.g., because this is the first run after install, or after an
// upgrade that added a sound), then store those.

for (final sound in soundsTodo) {
try {
final url = await _androidHost.copyNotificationSoundToMediaStore(
fileName: sound.fileDisplayName, resourceName: sound.resourceName);

if (sound == kDefaultNotificationSound) {
defaultSoundUrl = url;
}
} catch (e, st) {
assert(debugLog("$e\n$st"));
}
}

return defaultSoundUrl;
}

/// Create our notification channel, if it doesn't already exist.
//
// NOTE when changing anything here: the changes will not take effect
Expand Down Expand Up @@ -78,13 +167,15 @@ class NotificationChannelManager {

// The channel doesn't exist. Create it.

final defaultSoundUrl = await _ensureInitNotificationSounds();

await _androidHost.createNotificationChannel(NotificationChannel(
id: kChannelId,
name: 'Messages', // TODO(i18n)
importance: NotificationImportance.high,
lightsEnabled: true,
soundResourceUrl: defaultSoundUrl,
vibrationPattern: kVibrationPattern,
// TODO(#340) sound
));
}
}
Expand Down

0 comments on commit a7ffb60

Please sign in to comment.