Skip to content

Commit

Permalink
add firebase cloud messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
solygambas committed Apr 19, 2022
1 parent df853a6 commit 9710e37
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 3 deletions.
9 changes: 7 additions & 2 deletions 20-egg-timer/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Egg Timer

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

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

Based on [Using Android Notifications](https://developer.android.com/codelabs/advanced-android-kotlin-training-notifications#0) by Google Codelabs (2022).
Based on 2 tutorials by Google Codelabs (2022):

- [Using Android Notifications](https://developer.android.com/codelabs/advanced-android-kotlin-training-notifications#0)
- [Android Firebase Cloud Messaging](https://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-notifications-fcm)
2 changes: 2 additions & 0 deletions 20-egg-timer/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ dependencies {
implementation 'com.google.firebase:firebase-messaging:19.0.1'
implementation 'android.arch.work:work-runtime:1.0.1'
}

apply plugin: 'com.google.gms.google-services'
27 changes: 27 additions & 0 deletions 20-egg-timer/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@
android:enabled="true"
android:exported="false">
</receiver>
<!-- TODO: Step 3.0 uncomment to start the service -->
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!--&lt;!&ndash; [START fcm_default_icon] &ndash;&gt;-->
<!--&lt;!&ndash;-->
<!--Set custom default icon. This is used when no icon is set for incoming notification messages.-->
<!--See README(https://goo.gl/l4GJaQ) for more.-->
<!--&ndash;&gt;-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/common_google_signin_btn_icon_dark"/>
<!--&lt;!&ndash;-->
<!--Set color used with incoming notification messages. This is used when no color is set for the incoming-->
<!--notification message. See README(https://goo.gl/6BKBk7) for more.-->
<!--&ndash;&gt;-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent"/> <!-- [END fcm_default_icon] -->
<!-- [START fcm_default_channel] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/breakfast_notification_channel_id" />

</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.eggtimernotifications

import android.app.NotificationManager
import android.util.Log
import androidx.core.content.ContextCompat
import com.example.android.eggtimernotifications.util.sendNotification
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.d(TAG, "From: ${remoteMessage?.from}")

// TODO Step 3.5 check messages for data
// Check if message contains a data payload.


// TODO Step 3.6 check messages for notification and call sendNotification
// Check if message contains a notification payload.

}
// [END receive_message]

// tested with pixel 3 api 30
// your emulator needs google play + google apis to receive notifications
// reference: https://stackoverflow.com/questions/46464356/firebase-message-not-received-on-emulator/69045105#69045105
override fun onNewToken(token: String?) {
Log.d(TAG, "Refreshed token: $token")
sendRegistrationToServer(token)
super.onNewToken(token)
}
// [END on_new_token]

/**
* Persist token to third-party (your app) servers.
*
* @param token The new token.
*/
private fun sendRegistrationToServer(token: String?) {
// TODO: Implement this method to send token to your app server.
}

/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private fun sendNotification(messageBody: String) {
val notificationManager = ContextCompat.getSystemService(applicationContext, NotificationManager::class.java) as NotificationManager
notificationManager.sendNotification(messageBody, applicationContext)
}

companion object {
private const val TAG = "MyFirebaseMsgService"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class EggTimerFragment : Fragment() {
getString(R.string.egg_notification_channel_name)
)

// create another channel for fcm
createChannel(
getString(R.string.breakfast_notification_channel_id),
getString(R.string.breakfast_notification_channel_name)
)

return binding.root
}

Expand Down
1 change: 1 addition & 0 deletions 20-egg-timer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
| 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. |
| 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. |
| 19 | [Treasure Hunt](https://github.com/solygambas/kotlin-projects/tree/main/19-treasure-hunt) | A real-world scavenger hunt using geofencing. |
| 20 | [Egg Timer](https://github.com/solygambas/kotlin-projects/tree/main/20-egg-timer) | A timer app for cooking eggs, using notifications. |
| 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. |

These projects are adapted from various sources:

Expand Down

0 comments on commit 9710e37

Please sign in to comment.