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

[Android|iOS] overwrite system sound for push notifications via a custom one #36807

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
Expand All @@ -31,6 +33,7 @@
import androidx.core.graphics.drawable.IconCompat;
import androidx.versionedparcelable.ParcelUtils;

import com.expensify.chat.R;
import com.urbanairship.AirshipConfigOptions;
import com.urbanairship.json.JsonMap;
import com.urbanairship.json.JsonValue;
Expand Down Expand Up @@ -105,6 +108,9 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @
builder.setChannelId(CHANNEL_MESSAGES_ID);
} else {
builder.setPriority(PRIORITY_MAX);
// Set sound for versions below Oreo
// for Oreo and above we set sound on the notification's channel level
builder.setSound(getSoundFile(context));
}

// Attempt to parse data and apply custom notification styling
Expand All @@ -130,6 +136,13 @@ private void createAndRegisterNotificationChannel(@NonNull Context context) {
NotificationChannelGroup channelGroup = new NotificationChannelGroup(NOTIFICATION_GROUP_CHATS, CHANNEL_GROUP_NAME);
NotificationChannel channel = new NotificationChannel(CHANNEL_MESSAGES_ID, CHANNEL_MESSAGES_NAME, NotificationManager.IMPORTANCE_HIGH);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();

channel.setSound(getSoundFile(context), audioAttributes);

NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannelGroup(channelGroup);
notificationManager.createNotificationChannel(channel);
Expand Down Expand Up @@ -333,4 +346,8 @@ private Bitmap fetchIcon(@NonNull Context context, String urlString) {

return null;
}

private Uri getSoundFile(Context context) {
return Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.receive);
}
}
2 changes: 2 additions & 0 deletions ios/NotificationServiceExtension/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class NotificationService: UANotificationServiceExtension {
return
}

bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName("receive.mp3"))

if #available(iOSApplicationExtension 15.0, *) {
configureCommunicationNotification(notificationContent: bestAttemptContent, contentHandler: contentHandler)
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/libs/Sound/playSoundExcludingMobile/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// mobile platform plays a sound when notification is delivered (in native code)
export default function playSoundExcludingMobile() {}
5 changes: 5 additions & 0 deletions src/libs/Sound/playSoundExcludingMobile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import playSound from '..';

const playSoundExcludingMobile: typeof playSound = (sound) => playSound(sound);

export default playSoundExcludingMobile;
7 changes: 4 additions & 3 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import * as Pusher from '@libs/Pusher/pusher';
import PusherUtils from '@libs/PusherUtils';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import playSound, {SOUNDS} from '@libs/Sound';
import playSoundExcludingMobile from '@libs/Sound/playSoundExcludingMobile';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -527,12 +528,12 @@ function playSoundForMessageType(pushJSON: OnyxServerUpdate[]) {

// mention user
if ('html' in message && typeof message.html === 'string' && message.html.includes(`<mention-user>@${currentEmail}</mention-user>`)) {
return playSound(SOUNDS.ATTENTION);
return playSoundExcludingMobile(SOUNDS.ATTENTION);
}

// mention @here
if ('html' in message && typeof message.html === 'string' && message.html.includes('<mention-here>')) {
return playSound(SOUNDS.ATTENTION);
return playSoundExcludingMobile(SOUNDS.ATTENTION);
}

// assign a task
Expand All @@ -552,7 +553,7 @@ function playSoundForMessageType(pushJSON: OnyxServerUpdate[]) {

// plain message
if ('html' in message) {
return playSound(SOUNDS.RECEIVE);
return playSoundExcludingMobile(SOUNDS.RECEIVE);
}
}
} catch (e) {
Expand Down
Loading