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 @@ -106,6 +109,9 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @
builder.setPriority(PRIORITY_MAX);
}

// Set sound for versions below Oreo
builder.setSound(getSoundFile(context));
kirillzyusko marked this conversation as resolved.
Show resolved Hide resolved

// Attempt to parse data and apply custom notification styling
if (message.containsKey(PAYLOAD_KEY)) {
try {
Expand All @@ -129,6 +135,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 @@ -332,4 +345,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
10 changes: 8 additions & 2 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isBefore} from 'date-fns';
import {Platform} from 'react-native';
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
Expand Down Expand Up @@ -550,8 +551,13 @@ function playSoundForMessageType(pushJSON: OnyxServerUpdate[]) {
return playSound(SOUNDS.SUCCESS);
}

// plain message
if ('html' in message) {
if (
// plain message
'html' in message &&
// exclude mobile platforms, since they play sound when notification is delivered (in native code)
Platform.OS !== 'android' &&
Platform.OS !== 'ios'
kirillzyusko marked this conversation as resolved.
Show resolved Hide resolved
) {
return playSound(SOUNDS.RECEIVE);
}
}
Expand Down
Loading