본문 바로가기

프로그래밍/Android

Android Firebase FCM

Firebase Cloud Messaging

먼저 앱과 Firebase 를 연결한다. 
앱을 처음 실행할 때 FirebaseInstanceId로 token을 발급받는다. 
그리고 push 알림을 받을 service를 생성한다. 
service는 안드로이드 백그라운드에서 
실행되는 작업을 수행할 수 있는 애플리케이션 4대 구성요소 중 하나로 
UI를 제공하지 않는다는 특징이 있다. 

 

AndroidMenifest.xml 에 
Service를 명시하고 
intent-filter에 Fireabase Messaging Event를 작성한다.

meta-data 로
default app icon과 channel id 를 명시할 수 있고, 
token이 자동초기화 되는걸 방지할 수 있다. 

 

        <service android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_setting_house" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/channel_id_default" />

        <!--    자동 초기화 방지     -->
        <meta-data
            android:name="firebase_messaging_auto_init_enabled"
            android:value="false" />
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="false" />


Service class에서 

onNewToken() 에서는 새로운 토큰을 받을 경우 서버로 전송한다. 
onMessageReceived() 는 
새로운 메시지를 받을 경우 알림으로 표시한다. 

 

 

기본적인 설명은 여느 블로그에도 잘나와있다. 

나의 문제는 Background 상태일때 
받은 알림을 클릭했을때 원하는 액티비티가 열리지 않는 것.

 

1. background 상태일 때 받은 notification 을 
클릭했을때 원하는 acitivity로 실행하게 하거나 
받은 데이터 대로 동작하게 하는 방법

2. 프로세스가 종료된 상태에서 notification
이 오지 않는다. 
system tray에 notification 이 나타나지 않고
앱이 실행되면 온다. 


Foreground 는 앱이 화면 위에 띄워져있는 상태를 말하고, 
Background 는 앱이 화면에 없는 상태를 말한다. 
Kill 은 앱의 프로세스가 완전히 종료된 상태를 말한다. 

서비스란 백그라운드 상태에서 실행되는 것을 의미하지만 
O 버전부터 Kill 된 서비스의 백그라운드 실행을 금지시켰다. 

아직 서버쪽 개발이 안되어있어 PostMan으로 테스트했다. 

 



forground 상태일때는 잘 됐는데 background 상태일 때는 제대로 동작하지 않았다 .

 

{
	"notification": {
    	"title": "this is title 4",
    	"body": "8가지 핵심 개념을 리액티브 프로그래밍 서버나 애플리케이션", 
        "click_action" : ".NotificationActivity"
    },
	"to":"<token>",
	"priority":"high",
	"data":{
		"urlLink":"http://showmiso.tistory.com/40"
	}
}


{
	"data": {
    	"title": "this is title 8",
    	"body": "8가지 핵심 개념을 리액티브 프로그래밍 서버나 애플리케이션", 
        "click_action" : ".LoginActivity",
		"urlLink":"http://showmiso.tistory.com/40"
    },
	"to":"<token>",
	"priority":"high"
}


 


"notification" 을 "data" 로 옮겨줬다.

 


Kill 된 서비스에 알림을 날리고 싶다면, 

 

{
	"data": {
    	"title": "this is title 5",
    	"body": "8가지 핵심 개념을 리액티브 프로그래밍 서버나 애플리케이션", 
        "click_action" : ".NotificationActivity"
    },
	"to":"<token>",
	"priority":"high",
	"notification":{
		"urlLink":"http://showmiso.tistory.com/40"
	}
}

 



이렇게 하면 된다. 
여기서도 문제는 있다 이상태에서 backPressed 를 하면 앱이 종료가 된다.