Skip to content

Commit 9710e37

Browse files
committed
add firebase cloud messaging
1 parent df853a6 commit 9710e37

File tree

7 files changed

+124
-3
lines changed

7 files changed

+124
-3
lines changed

20-egg-timer/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Egg Timer
22

3-
A timer app for cooking eggs, using notifications.
3+
A timer app for cooking eggs, using notifications and Firebase Cloud Messaging.
44

55
<!-- <p align="center">
66
<img src="screenshot.png" style="width:528px;max-width: 100%;">
@@ -14,5 +14,10 @@ A timer app for cooking eggs, using notifications.
1414
- customizing the notifications.
1515
- adding quick actions to make the notification interactive.
1616
- turning off notification badges.
17+
- pushing messages to the user via Firebase Cloud Messaging.
18+
- transfering data from a backend to the app.
1719

18-
Based on [Using Android Notifications](https://developer.android.com/codelabs/advanced-android-kotlin-training-notifications#0) by Google Codelabs (2022).
20+
Based on 2 tutorials by Google Codelabs (2022):
21+
22+
- [Using Android Notifications](https://developer.android.com/codelabs/advanced-android-kotlin-training-notifications#0)
23+
- [Android Firebase Cloud Messaging](https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-notifications-fcm)

20-egg-timer/app/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ dependencies {
6161
implementation 'com.google.firebase:firebase-messaging:19.0.1'
6262
implementation 'android.arch.work:work-runtime:1.0.1'
6363
}
64+
65+
apply plugin: 'com.google.gms.google-services'

20-egg-timer/app/src/main/AndroidManifest.xml

+27
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@
4444
android:enabled="true"
4545
android:exported="false">
4646
</receiver>
47+
<!-- TODO: Step 3.0 uncomment to start the service -->
48+
<service
49+
android:name=".MyFirebaseMessagingService"
50+
android:exported="false">
51+
<intent-filter>
52+
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
53+
</intent-filter>
54+
</service>
55+
<!--&lt;!&ndash; [START fcm_default_icon] &ndash;&gt;-->
56+
<!--&lt;!&ndash;-->
57+
<!--Set custom default icon. This is used when no icon is set for incoming notification messages.-->
58+
<!--See README(https://goo.gl/l4GJaQ) for more.-->
59+
<!--&ndash;&gt;-->
60+
<meta-data
61+
android:name="com.google.firebase.messaging.default_notification_icon"
62+
android:resource="@drawable/common_google_signin_btn_icon_dark"/>
63+
<!--&lt;!&ndash;-->
64+
<!--Set color used with incoming notification messages. This is used when no color is set for the incoming-->
65+
<!--notification message. See README(https://goo.gl/6BKBk7) for more.-->
66+
<!--&ndash;&gt;-->
67+
<meta-data
68+
android:name="com.google.firebase.messaging.default_notification_color"
69+
android:resource="@color/colorAccent"/> <!-- [END fcm_default_icon] -->
70+
<!-- [START fcm_default_channel] -->
71+
<meta-data
72+
android:name="com.google.firebase.messaging.default_notification_channel_id"
73+
android:value="@string/breakfast_notification_channel_id" />
4774

4875
</application>
4976

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (C) 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.eggtimernotifications
18+
19+
import android.app.NotificationManager
20+
import android.util.Log
21+
import androidx.core.content.ContextCompat
22+
import com.example.android.eggtimernotifications.util.sendNotification
23+
import com.google.firebase.messaging.FirebaseMessagingService
24+
import com.google.firebase.messaging.RemoteMessage
25+
26+
class MyFirebaseMessagingService : FirebaseMessagingService() {
27+
28+
/**
29+
* Called when message is received.
30+
*
31+
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
32+
*/
33+
// [START receive_message]
34+
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
35+
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
36+
Log.d(TAG, "From: ${remoteMessage?.from}")
37+
38+
// TODO Step 3.5 check messages for data
39+
// Check if message contains a data payload.
40+
41+
42+
// TODO Step 3.6 check messages for notification and call sendNotification
43+
// Check if message contains a notification payload.
44+
45+
}
46+
// [END receive_message]
47+
48+
// tested with pixel 3 api 30
49+
// your emulator needs google play + google apis to receive notifications
50+
// reference: https://stackoverflow.com/questions/46464356/firebase-message-not-received-on-emulator/69045105#69045105
51+
override fun onNewToken(token: String?) {
52+
Log.d(TAG, "Refreshed token: $token")
53+
sendRegistrationToServer(token)
54+
super.onNewToken(token)
55+
}
56+
// [END on_new_token]
57+
58+
/**
59+
* Persist token to third-party (your app) servers.
60+
*
61+
* @param token The new token.
62+
*/
63+
private fun sendRegistrationToServer(token: String?) {
64+
// TODO: Implement this method to send token to your app server.
65+
}
66+
67+
/**
68+
* Create and show a simple notification containing the received FCM message.
69+
*
70+
* @param messageBody FCM message body received.
71+
*/
72+
private fun sendNotification(messageBody: String) {
73+
val notificationManager = ContextCompat.getSystemService(applicationContext, NotificationManager::class.java) as NotificationManager
74+
notificationManager.sendNotification(messageBody, applicationContext)
75+
}
76+
77+
companion object {
78+
private const val TAG = "MyFirebaseMsgService"
79+
}
80+
}

20-egg-timer/app/src/main/java/com/example/android/eggtimernotifications/ui/EggTimerFragment.kt

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class EggTimerFragment : Fragment() {
5656
getString(R.string.egg_notification_channel_name)
5757
)
5858

59+
// create another channel for fcm
60+
createChannel(
61+
getString(R.string.breakfast_notification_channel_id),
62+
getString(R.string.breakfast_notification_channel_name)
63+
)
64+
5965
return binding.root
6066
}
6167

20-egg-timer/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ buildscript {
2424
dependencies {
2525
classpath 'com.android.tools.build:gradle:7.0.3'
2626
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
27+
classpath 'com.google.gms:google-services:4.3.2'
2728
// NOTE: Do not place your application dependencies here; they belong
2829
// in the individual module build.gradle files
2930
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
| 17 | [Fun Facts](https://github.com/solygambas/kotlin-projects/tree/main/17-fun-facts) | An app that displays fun facts about Android, using FirebaseUI Authentication. |
2424
| 18 | [Wander](https://github.com/solygambas/kotlin-projects/tree/main/18-wander) | A Google Maps app that displays customized maps and the user's location. |
2525
| 19 | [Treasure Hunt](https://github.com/solygambas/kotlin-projects/tree/main/19-treasure-hunt) | A real-world scavenger hunt using geofencing. |
26-
| 20 | [Egg Timer](https://github.com/solygambas/kotlin-projects/tree/main/20-egg-timer) | A timer app for cooking eggs, using notifications. |
26+
| 20 | [Egg Timer](https://github.com/solygambas/kotlin-projects/tree/main/20-egg-timer) | A timer app for cooking eggs, using notifications and Firebase Cloud Messaging. |
2727

2828
These projects are adapted from various sources:
2929

0 commit comments

Comments
 (0)