Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the foundation for remote push notifications #57

Merged
merged 21 commits into from Sep 28, 2020
Merged
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions app/build.gradle
Expand Up @@ -58,10 +58,11 @@ dependencies {
implementation 'io.coil-kt:coil:0.9.5'

// Firebase
implementation 'com.google.firebase:firebase-analytics:17.4.4'
implementation 'com.google.firebase:firebase-crashlytics:17.1.1'
implementation 'com.google.firebase:firebase-analytics:17.5.0'
uhooi marked this conversation as resolved.
Show resolved Hide resolved
implementation 'com.google.firebase:firebase-crashlytics:17.2.1'
implementation 'com.google.firebase:firebase-firestore-ktx:21.6.0'
implementation 'com.google.firebase:firebase-messaging:20.2.4'
implementation 'com.google.firebase:firebase-perf:19.0.8'
implementation 'com.google.firebase:firebase-firestore-ktx:21.5.0'

implementation 'androidx.recyclerview:recyclerview:1.1.0'

Expand Down
12 changes: 11 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
uhooi marked this conversation as resolved.
Show resolved Hide resolved

<application
android:name=".MainApplication"
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
Expand All @@ -20,6 +21,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".fcm.MyFirebaseMessagingService"
uhooi marked this conversation as resolved.
Show resolved Hide resolved
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="@mipmap/ic_notification" />
</application>

</manifest>
41 changes: 41 additions & 0 deletions app/src/main/java/com/theuhooi/uhooipicbook/MainApplication.kt
@@ -0,0 +1,41 @@
package com.theuhooi.uhooipicbook

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context

class MainApplication : Application() {
uhooi marked this conversation as resolved.
Show resolved Hide resolved

// region View Life-Cycle Methods

override fun onCreate() {
super.onCreate()
createNotificationChannels()
}

// endregion

// region Other Private Methods

private fun createNotificationChannels() {
val channels = listOf(
createInfoNotificationChannel()
)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
channels.forEach { notificationManager.createNotificationChannel(it) }
}

private fun createInfoNotificationChannel(): NotificationChannel {
val id = getString(R.string.info_notification_channel_id)
val name = getString(R.string.info_notification_channel_name)
val importance = NotificationManager.IMPORTANCE_HIGH
return NotificationChannel(id, name, importance).apply {
description = getString(R.string.info_notification_channel_description)
}
}

// endregion

}
@@ -0,0 +1,60 @@
package com.theuhooi.uhooipicbook.fcm

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.theuhooi.uhooipicbook.MainActivity
import com.theuhooi.uhooipicbook.R

class MyFirebaseMessagingService : FirebaseMessagingService() {

// region Stored Instance Properties

private val infoNotificationId = 0

// endregion

// region FirebaseMessagingService

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)

message.notification?.let {
sendInfoNotification(it.title ?: getString(R.string.app_name), it.body ?: "")
}
}

// endregion

// region Other Private Methods

private fun sendInfoNotification(title: String, text: String) {
sendNotification(title, text, getString(R.string.info_notification_channel_id), this.infoNotificationId)
}

private fun sendNotification(title: String, text: String, channelId: String, notificationId: Int) {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(title)
.setContentText(text)
.setStyle(NotificationCompat.BigTextStyle().bigText(text))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.build()
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notification)
}
}
Binary file added app/src/main/res/mipmap-hdpi/ic_notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-mdpi/ic_notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/ic_notification.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -2,6 +2,10 @@
<string name="app_name">UhooiPicBook</string>
<string name="icon_description">Icon</string>

<string name="info_notification_channel_id">info</string>
<string name="info_notification_channel_name">お知らせ</string>
<string name="info_notification_channel_description">お知らせ</string>

<string name="name_dummy">uhooi</string>
<string name="description_dummy">ゆかいな みどりの せいぶつ。\nわるそうに みえるが むがい。</string>
</resources>
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -2,7 +2,7 @@

buildscript {
ext {
kotlin_version = '1.3.72'
kotlin_version = '1.4.10'
nav_version = '2.3.0'
detekt_version = '1.7.4'
}
Expand All @@ -17,7 +17,7 @@ buildscript {

// Firebase
classpath 'com.google.gms:google-services:4.3.3'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
classpath 'com.google.firebase:perf-plugin:1.3.1'

classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detekt_version"
Expand Down