⭐️ Native 코드 수정을 통한 해결방법 ⭐️
1. 환경
react-native: 0.61.5
react-native-push-notification: 4.0.0
2. 문제
위 라이브러리를 이용해서 알람앱을 만들었다.
하지만 안드로이드 기기에서 진동/무음모드를 설정하면 알람음을 설정해놔도 울리지 않았다.
나의 코드는 이렇다.
import PushNotification, { PushNotificationScheduleObject } from 'react-native-push-notification';
const date = new Date();
date.setMinutes(date.getMinutes() + 1); // 1분 뒤에 알람 울리도록
const scheduleObj: PushNotificationScheduleObject = {
id: '1', // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
date,
autoCancel: false, // (optional) default: true
vibrate: true, // (optional) default: true
vibration: 2000, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
ongoing: false, // (optional) set whether this is an "ongoing" notification
priority: 'max', // (optional) set notification priority, default: high
visibility: 'public', // (optional) set notification visibility, default: private
importance: 'max', // (optional) set notification importance, default: high
title: 'title',
message: 'message',
playSound: true,
soundName: 'default_alarm_sound_twice', // android/app/src/main/res/raw 에 'default_alarm_sound_twice.mp3'라는 custom sound가 있다.
repeatType: 'time',
repeatTime: 4000,
}
PushNotification.localNotificationSchedule(scheduleObj);
3. 해결법 모색
이슈를 올려보았다.
도움이 되지 않았다.
알람음은 어떻게 해도 진동/무음모드에서 바뀌지 않았다.
그래서 미디어 볼륨을 건드려보기로 했다.
이곳 에다가
try {
if (soundUri != null) {
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
(int)(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 0.7),
AudioManager.FLAG_PLAY_SOUND);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(context, soundUri);
mediaPlayer.prepare();
mediaPlayer.start();
}
} catch (Exception e) {
Log.d("LOG", e.toString());
}
이 코드를 집어 넣는 것이다.
결국 네이티브 코드를 수정했다.
아예 MediaPlayer를 이용해서 내가 설정한 custom 사운드를 울려버리는 것이다.
npm i 마다 매번 이 코드를 추가해주어야 하겠지만 (내 레포지토리를 npm i 하는 방법을 몰라서.. 나중에 해보자)
어쨌든 해결됐다!
추가로 작성한 이슈 에 내가 방금 위에 써놓은 해결법을 그대로 적어놓았다.
해결되었다!
push-notification 라이브러리로도 진동/무음모드 때 소리가 울리는 알람을 구현할 수 있게 되었다.
4. 참고 블로그
https://unikys.tistory.com/350
[Android(안드로이드) 앱 개발 기초] MediaPlayer 음악 재생하기
* 이번에는 음악 재생을 위하여 사용하게 되는 MediaPlayer의 개발자 매뉴얼을 살펴보도록 하자. 음악을 재생할 때 주의할 점들과 각종 상황들에 대한 다양한 팁들을 포함하고 있어서 읽어보면 매��
unikys.tistory.com
https://15051015.tistory.com/93
안드로이드 볼륨 조절하기 (원하는 볼륨값으로 세팅하기)
안드로이드를 사용하다가 벨소리, 미디어가 아닌 알람 등을 조절하고 싶은 경우가 생긴다. 그럴경우에는 AudioManager 를 사용하여 조절할 수 있다. 아래에 있는 함수를 Activity 클래스 안에 추가해��
15051015.tistory.com
'Solve Problem > Troubleshooting' 카테고리의 다른 글
플레이스토어 Apk 업로드 시 => "업로드할 수 없습니다. 다시 시도해 보세요" 에러 (0) | 2020.11.05 |
---|---|
[React Native / Android] 푸시알람 절전모드로 인해 울리지 않을 때 (0) | 2020.09.02 |
스플래시 스크린 작동 안함 / Splash Screen not working (2) | 2020.07.15 |
react-native-push-notification: Android에서 custom sound가 안울림 / android userInfo 오류 (0) | 2020.07.14 |
apk 설치하면 알 수 없는 크래시 (0) | 2020.07.11 |