본문 바로가기

React&React-Native/RN-Cuckoo

[#React-Native] Node, react-native-push-notification를 이용한 새 메시지 알람 기능

반응형

 

 

 

 

 

 

firebase에서 cloud-messaging을 이용해 전체 공지 같은 알람 보내기는 성공했지만

메시지 탭에서 새로운 메시지가 들어왔을 시 알림을 구현하는 방법을 찾기가 힘들었다

그래서 react-native와 Node를 이용해서 알림을 구현...!

 

 

 

 

 

① Node를 구현할 디렉토리 생성 후 npm으로 설치

 

 

 

 

 

 

② Firebase HOME -> 설정 -> 프로젝트 설정

 

 

 

 

 

 

③ 서비스 계정-> Node.js 선택 후 새 비공개 키 생성 후 디렉토리 안에 생성된 json 파일을 넣는다

 

 

 

 

 

 

④ app.js 세팅

 

const express = require('express');
const admin = require('firebase-admin');
const serviceAccount = require('./project_name_file.json');
const functions = require('firebase-functions');

const schedule = require("node-schedule");

 

admin.initializeApp();

 

const app = express();

 

app.listen(3000, function(){

   schedule.scheduleJob('*/5 * * * * *', function() { 

      const message = { 

          android: {
               priority: 'normal',
               notification: {
                    body: '새로운 메시지가 도착했습니다',
                    icon: 'ic_push',                     

                    sound: 'default',
                    click_action: 'OPEN_ACTIVITY',
                    }
               },
          token: tokenValue,
      };

  });

});

 

 

 

 

5초마다 테이블에 새로운 메시지가 있으면 푸시 설정 on/off 여부를 확인 후

알림을 보내고 데이터를 삭제하는 방식을 사용했다

알림은 무사히 성공했는데 애를 먹은 건 어플이 종료된 상태일 때 알림이 가고 그 알림을 클릭하면 오류가 발생하는 것...!

notification의 click_action 으로 해결했다

 

react-native 폴더로 돌아가서

 

 

 

 

 

⑤ Project\android\app\src\main/AndroidManifest.xml

 

 

 

 

 

 

<activity></activity> 사이에 위의 코드를 추가해준다

notification에서 click_action = 'OPEN_ACTIVITY'로 앱을 실행하게 된다

 

 

 

 

 

반응형