포트폴리오

[소개] FMNotification (Android Library : Push(FCM) Receiver)

freemmer 2019. 1. 28. 17:46
FMNotification 는 ‘Push 수신 및 처리’를 쉽게 구현할 수 있도록 도와주는 라이브러리 입니다.
아래의 스크린샷과 같이 Android P에서 Heads up notification 동작하며, Firebase의 'Notification Type'의 메세지와 'Data Type'의 메세지 모두를 지원합니다.

해당 프로젝트는 'https://github.com/firebase/quickstart-android/tree/master/messaging'를 기반으로 작업되었습니다.
  • Platform             : Android
  • Language             : Kotlin
  • Type                 : Library 
  • Support SDK Version  : 19+




# 사용방법

★ 기본적으로 FCM 사용준비가 되어 있어야 합니다. (Firebase에 FCM을 사용할 앱을 등록하고 google-services.json을 발급받아 적용)
Project build.gradle 에 아래 항목을 추가합니다.

Project build.gradle 에 아래 항목을 추가합니다.
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
App build.gradle 에 아래 항목을 추가합니다.
현재 최신 버전은 1.0.0 입니다. https://github.com/freemmer/FMNotification 에서 최신 버전을 확인 하실 수 있습니다.
dependencies {
...
implementation 'com.github.freemmer:FMNotification:1.0.0'
}

사용방법

class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
// When only payload type is used like 'GCM'
FMNotification.PAYLOAD_TITLE_KEY = "title"
FMNotification.PAYLOAD_BODY_KEY = "body"
// In case automatically create default channel
FMNotification.initialize(this)
// In case manually create default channel
//FMNotification.instance(this).createChannel("default_channel_id", "Default Channel")
}
}
Application 혹은 Launch Activity에서 초기화를 호출하여, 기본 채널을 생성합니다.
FMNotification.initialize(this)을 호출하면 내부적으로 기본 채널을 생성하며, 직접 채널을 생성하실 수도 있습니다.
서버가 기존 GCM방식과 같이 동작한다면, 제목과 메세지의 Key 정보를 입력해 주셔야 하며, 그 이후 동작은 두 방식 모두 동일하게 onClickedNotification()에서 처리됩니다.
※ firebase의 메세지 방식은 아래 포스팅을 참고해 주시기 바랍니다.
class MainActivity : FMNotificationAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
logTokenButton.setOnClickListener {
// Get push token
FMNotification.getPushToken { isSuccessful: Boolean, token: String? ->
if (!isSuccessful) {
Log.w(TAG, "getInstanceId failed")
} else {
val msg = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
}
}
}
}
override fun onClickedNotification(bundle: Bundle) {
// In case a notification message is tapped.
// Handle possible data accompanying notification message.
for (key in bundle.keySet()) {
val value = bundle.get(key)
Log.d(TAG, "Key: $key Value: $value")
}
}
}
Heads up Notification 및 Notification 은 자동으로 처리되며, 위의 예제와 같이 Notification을 탭했을때 처리할 클래스에서 FMNotificationAppCompatActivity등을 상속받는 것으로 해결 됩니다.
Activity의 종류는 다음과 같습니다.
  • FMNotificationActivity            : Activity를 상속 받음
  • FMNotificationAppCompatActivity   : AppCompatActivity를 상속 받음
  • FMNotificationFragmentActivity    : FragmentActivity를 상속 받음


반응형