Skip to content

Commit 0bbcb5d

Browse files
committed
add topics, data payload and foreground notifications
1 parent 9710e37 commit 0bbcb5d

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

Diff for: 20-egg-timer/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
A timer app for cooking eggs, using notifications and Firebase Cloud Messaging.
44

5-
<!-- <p align="center">
5+
<p align="center">
66
<img src="screenshot.png" style="width:528px;max-width: 100%;">
7-
</p> -->
7+
</p>
88

99
## Features
1010

@@ -15,7 +15,7 @@ A timer app for cooking eggs, using notifications and Firebase Cloud Messaging.
1515
- adding quick actions to make the notification interactive.
1616
- turning off notification badges.
1717
- pushing messages to the user via Firebase Cloud Messaging.
18-
- transfering data from a backend to the app.
18+
- transferring data from a backend to the app.
1919

2020
Based on 2 tutorials by Google Codelabs (2022):
2121

Diff for: 20-egg-timer/app/src/main/java/com/example/android/eggtimernotifications/MyFirebaseMessagingService.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
3535
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
3636
Log.d(TAG, "From: ${remoteMessage?.from}")
3737

38-
// TODO Step 3.5 check messages for data
38+
// Step 3.5 check messages for data
3939
// Check if message contains a data payload.
40+
remoteMessage?.data?.let {
41+
Log.d(TAG, "Message data payload: " + remoteMessage.data)
42+
}
4043

41-
42-
// TODO Step 3.6 check messages for notification and call sendNotification
44+
// Step 3.6 check messages for notification and call sendNotification
4345
// Check if message contains a notification payload.
46+
remoteMessage?.notification?.let {
47+
Log.d(TAG, "Message Notification Body: ${it.body}")
48+
sendNotification(it.body as String)
49+
}
4450

4551
}
4652
// [END receive_message]
@@ -61,7 +67,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
6167
* @param token The new token.
6268
*/
6369
private fun sendRegistrationToServer(token: String?) {
64-
// TODO: Implement this method to send token to your app server.
70+
// Optional: Implement this method to send token to your app server.
6571
}
6672

6773
/**

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

+15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class EggTimerFragment : Fragment() {
6262
getString(R.string.breakfast_notification_channel_name)
6363
)
6464

65+
// call subscribe topics on start
66+
subscribeTopic()
67+
6568
return binding.root
6669
}
6770

@@ -92,6 +95,18 @@ class EggTimerFragment : Fragment() {
9295
// Step 1.6 END create a channel
9396
}
9497

98+
// subscribe to breakfast topic
99+
private fun subscribeTopic() {
100+
FirebaseMessaging.getInstance().subscribeToTopic(TOPIC)
101+
.addOnCompleteListener { task ->
102+
var msg = getString(R.string.message_subscribed)
103+
if (!task.isSuccessful) {
104+
msg = getString(R.string.message_subscribe_failed)
105+
}
106+
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
107+
}
108+
}
109+
95110
companion object {
96111
fun newInstance() = EggTimerFragment()
97112
}

Diff for: 20-egg-timer/screenshot.png

176 KB
Loading

0 commit comments

Comments
 (0)