From cb77586868d4c5f23a3a5bea6bf1da3b6799ba66 Mon Sep 17 00:00:00 2001 From: Nolan Kaplan Date: Tue, 11 Dec 2018 16:38:38 -0500 Subject: [PATCH 1/3] take sound param, don't crash if priority not set --- .../evollu/react/fcm/FIRMessagingModule.java | 56 +++++++++++++------ index.d.ts | 1 + 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index e1e58536..937e7075 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -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; @@ -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, @@ -118,6 +126,20 @@ 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); diff --git a/index.d.ts b/index.d.ts index f4b80201..b3a9ea70 100644 --- a/index.d.ts +++ b/index.d.ts @@ -165,6 +165,7 @@ declare module "react-native-fcm" { name: string; description?: string; priority?: string; + sound?: string; }); } From ce9c1ac1538abc325a24226bc9dc98e6102c0674 Mon Sep 17 00:00:00 2001 From: Nolan Kaplan Date: Tue, 11 Dec 2018 17:15:19 -0500 Subject: [PATCH 2/3] update README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a07c84c4..84245774 100644 --- a/README.md +++ b/README.md @@ -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' }); ``` From 96f426ee66d0de057fc43d69433c753e048360b3 Mon Sep 17 00:00:00 2001 From: Nolan Kaplan Date: Tue, 11 Dec 2018 17:19:36 -0500 Subject: [PATCH 3/3] code formatting --- .../src/main/java/com/evollu/react/fcm/FIRMessagingModule.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index 937e7075..8ee11476 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -136,7 +136,8 @@ public void createNotificationChannel(ReadableMap details, Promise promise){ + "://" + getReactApplicationContext().getPackageName() + "/raw/" - + sound); + + sound + ); channel.setSound(soundUri, attributes); }