Skip to content

Commit 6d20997

Browse files
authored
Merge pull request #36807 from margelo/fix/36579-sound-from-push-notification
[Android|iOS] overwrite system sound for push notifications via a custom one
2 parents 7120be2 + 9e9476e commit 6d20997

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

android/app/src/main/java/com/expensify/chat/customairshipextender/CustomNotificationProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import android.graphics.PorterDuff.Mode;
1616
import android.graphics.PorterDuffXfermode;
1717
import android.graphics.Rect;
18+
import android.media.AudioAttributes;
19+
import android.net.Uri;
1820
import android.os.Build;
1921
import android.os.Bundle;
2022
import android.service.notification.StatusBarNotification;
@@ -31,6 +33,7 @@
3133
import androidx.core.graphics.drawable.IconCompat;
3234
import androidx.versionedparcelable.ParcelUtils;
3335

36+
import com.expensify.chat.R;
3437
import com.urbanairship.AirshipConfigOptions;
3538
import com.urbanairship.json.JsonMap;
3639
import com.urbanairship.json.JsonValue;
@@ -105,6 +108,9 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @
105108
builder.setChannelId(CHANNEL_MESSAGES_ID);
106109
} else {
107110
builder.setPriority(PRIORITY_MAX);
111+
// Set sound for versions below Oreo
112+
// for Oreo and above we set sound on the notification's channel level
113+
builder.setSound(getSoundFile(context));
108114
}
109115

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

139+
AudioAttributes audioAttributes = new AudioAttributes.Builder()
140+
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
141+
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
142+
.build();
143+
144+
channel.setSound(getSoundFile(context), audioAttributes);
145+
133146
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
134147
notificationManager.createNotificationChannelGroup(channelGroup);
135148
notificationManager.createNotificationChannel(channel);
@@ -333,4 +346,8 @@ private Bitmap fetchIcon(@NonNull Context context, String urlString) {
333346

334347
return null;
335348
}
349+
350+
private Uri getSoundFile(Context context) {
351+
return Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.receive);
352+
}
336353
}

ios/NotificationServiceExtension/NotificationService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class NotificationService: UANotificationServiceExtension {
2424
return
2525
}
2626

27+
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName("receive.mp3"))
28+
2729
if #available(iOSApplicationExtension 15.0, *) {
2830
configureCommunicationNotification(notificationContent: bestAttemptContent, contentHandler: contentHandler)
2931
} else {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// mobile platform plays a sound when notification is delivered (in native code)
2+
export default function playSoundExcludingMobile() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import playSound from '..';
2+
3+
const playSoundExcludingMobile: typeof playSound = (sound) => playSound(sound);
4+
5+
export default playSoundExcludingMobile;

src/libs/actions/User.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as Pusher from '@libs/Pusher/pusher';
2929
import PusherUtils from '@libs/PusherUtils';
3030
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
3131
import playSound, {SOUNDS} from '@libs/Sound';
32+
import playSoundExcludingMobile from '@libs/Sound/playSoundExcludingMobile';
3233
import CONST from '@src/CONST';
3334
import ONYXKEYS from '@src/ONYXKEYS';
3435
import ROUTES from '@src/ROUTES';
@@ -527,12 +528,12 @@ function playSoundForMessageType(pushJSON: OnyxServerUpdate[]) {
527528

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

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

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

553554
// plain message
554555
if ('html' in message) {
555-
return playSound(SOUNDS.RECEIVE);
556+
return playSoundExcludingMobile(SOUNDS.RECEIVE);
556557
}
557558
}
558559
} catch (e) {

0 commit comments

Comments
 (0)