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

accept sound param when creating notification channel #1083

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ FCM.createNotificationChannel({
name: 'Car status',
description: 'Notifies when changes car status',
priority: 'max',
// set custom sound when the creating channel for Oreo and later
// place sound file in /android/app/src/main/res/raw
sound: 'bell'
});
```

Expand Down
57 changes: 40 additions & 17 deletions android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.content.ContentResolver;
import android.media.AudioAttributes;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
Expand Down Expand Up @@ -87,29 +90,34 @@ public void createNotificationChannel(ReadableMap details, Promise promise){
NotificationManager mngr = (NotificationManager) getReactApplicationContext().getSystemService(NOTIFICATION_SERVICE);
String id = details.getString("id");
String name = details.getString("name");
String priority = details.getString("priority");
int importance;
switch(priority) {
case "min":
importance = NotificationManager.IMPORTANCE_MIN;
break;
case "low":
importance = NotificationManager.IMPORTANCE_LOW;
break;
case "high":
importance = NotificationManager.IMPORTANCE_HIGH;
break;
case "max":
importance = NotificationManager.IMPORTANCE_MAX;
break;
default:
importance = NotificationManager.IMPORTANCE_DEFAULT;
if (details.hasKey("priority")) {
String priority = details.getString("priority");
switch (priority) {
case "min":
importance = NotificationManager.IMPORTANCE_MIN;
break;
case "low":
importance = NotificationManager.IMPORTANCE_LOW;
break;
case "high":
importance = NotificationManager.IMPORTANCE_HIGH;
break;
case "max":
importance = NotificationManager.IMPORTANCE_MAX;
break;
default:
importance = NotificationManager.IMPORTANCE_DEFAULT;
}
}
else {
importance = NotificationManager.IMPORTANCE_DEFAULT;
}
if (mngr.getNotificationChannel(id) != null) {
promise.resolve(null);
return;
}
//

NotificationChannel channel = new NotificationChannel(
id,
name,
Expand All @@ -118,6 +126,21 @@ public void createNotificationChannel(ReadableMap details, Promise promise){
if(details.hasKey("description")){
channel.setDescription(details.getString("description"));
}
if (details.hasKey("sound")) {
String sound = details.getString("sound");
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();

Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://"
+ getReactApplicationContext().getPackageName()
+ "/raw/"
+ sound
);
channel.setSound(soundUri, attributes);
}

mngr.createNotificationChannel(channel);
}
promise.resolve(null);
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ declare module "react-native-fcm" {
name: string;
description?: string;
priority?: string;
sound?: string;
});
}

Expand Down