728x90
No channel id passed, notifications may not work.
예전에 만들어둔 푸시 부분이 갑자기 포어그라운드에서 동작하지 않는 것을 확인했다.
처음에는 서버 오류인 줄 알고 서버를 확인해보았으나 정상 작동하였고, 경우의 수를 테스트해본 결과 포어그라운드의 문제였다.
안드로이드 오레오(API 26) 이상에서는 푸시 알림을 설정할 때 채널을 꼭 생성해주어야 하는데, 작업한 지 조금 된 프로젝트기도 하고 테스트 기기가 하필 저버전이어서 이 부분을 놓쳤다.
해결 방법)
앱 실행 시 createChannel을 통해 채널을 생성해주면 된다.
라이브러리는 react-native-push-notification을 사용하였다.
import PushNotification, {Importance} from 'react-native-push-notification';
...
PushNotification.createChannel(
{
channelId: "channel-id", // (required)
channelName: "My channel", // (required)
channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
playSound: false, // (optional) default: true
soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
vibrate: true, // (optional) default: true. Creates the default vibration pattern if true.
},
(created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
);
channelId와 channelName 만 필수값이므로 나머지 사항들은 설정할 부분만 추가로 넣어주면 된다.
이 코드를 앱이 시작하는 부분에 생성해주면 완료된다.
300x250