From 9ea64fb46038cd58b7bfcc53db25c315b2238287 Mon Sep 17 00:00:00 2001 From: raphael Date: Thu, 30 Dec 2021 13:08:52 +0100 Subject: [PATCH] adds firebase connection and notifications - notification handler pushes background and foreground notifications to the phone directly - firebase connection pushes remote notifications in background and foreground - firebase unique token currently logged only, should be send directly to server via rest --- app/build.gradle | 5 +- .../bump/MessageProcessorTest.kt | 6 +- .../{example => maenle}/bump/RestCryptTest.kt | 8 +- app/src/main/AndroidManifest.xml | 26 ++++++- .../example/bump/receiver/AlarmReceiver.kt | 28 ------- .../example/bump/receiver/SnoozeReceiver.kt | 42 ---------- .../maenle/bump/MyFirebaseMessagingService.kt | 78 +++++++++++++++++++ .../bump/ui/CameraFragment.kt | 6 +- .../bump/ui/FirstFragment.kt | 41 +++++++--- .../bump/ui/MainActivity.kt | 2 +- .../bump/ui/MainApplication.kt | 6 +- .../bump/ui/SecondFragment.kt | 3 +- .../bump/util/BumpProcessor.kt | 2 +- .../bump/util/CameraXViewModel.kt | 2 +- .../bump/util/LocalData.kt | 2 +- .../{example => maenle}/bump/util/Message.kt | 3 +- .../bump/util/MessageProcessor.kt | 4 +- .../bump/util/NotificationUtil.kt | 30 +------ .../bump/util/RestSingleton.kt | 4 +- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/fragment_camera.xml | 2 +- app/src/main/res/layout/fragment_first.xml | 2 +- app/src/main/res/layout/fragment_second.xml | 2 +- app/src/main/res/menu/menu_main.xml | 2 +- app/src/main/res/navigation/nav_graph.xml | 6 +- app/src/main/res/values/strings.xml | 3 + .../bump/ExampleUnitTest.kt | 0 build.gradle | 4 +- 28 files changed, 179 insertions(+), 142 deletions(-) rename app/src/androidTest/java/com/{example => maenle}/bump/MessageProcessorTest.kt (94%) rename app/src/androidTest/java/com/{example => maenle}/bump/RestCryptTest.kt (95%) delete mode 100644 app/src/main/java/com/example/bump/receiver/AlarmReceiver.kt delete mode 100644 app/src/main/java/com/example/bump/receiver/SnoozeReceiver.kt create mode 100644 app/src/main/java/com/maenle/bump/MyFirebaseMessagingService.kt rename app/src/main/java/com/{example => maenle}/bump/ui/CameraFragment.kt (98%) rename app/src/main/java/com/{example => maenle}/bump/ui/FirstFragment.kt (69%) rename app/src/main/java/com/{example => maenle}/bump/ui/MainActivity.kt (98%) rename app/src/main/java/com/{example => maenle}/bump/ui/MainApplication.kt (87%) rename app/src/main/java/com/{example => maenle}/bump/ui/SecondFragment.kt (92%) rename app/src/main/java/com/{example => maenle}/bump/util/BumpProcessor.kt (99%) rename app/src/main/java/com/{example => maenle}/bump/util/CameraXViewModel.kt (98%) rename app/src/main/java/com/{example => maenle}/bump/util/LocalData.kt (95%) rename app/src/main/java/com/{example => maenle}/bump/util/Message.kt (83%) rename app/src/main/java/com/{example => maenle}/bump/util/MessageProcessor.kt (98%) rename app/src/main/java/com/{example => maenle}/bump/util/NotificationUtil.kt (69%) rename app/src/main/java/com/{example => maenle}/bump/util/RestSingleton.kt (97%) rename app/src/test/java/com/{example => maenle}/bump/ExampleUnitTest.kt (100%) diff --git a/app/build.gradle b/app/build.gradle index a9be92a..65faab4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,6 +38,7 @@ dependencies { implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:16.1.4' implementation 'com.android.volley:volley:1.2.0' + implementation 'com.google.firebase:firebase-messaging-ktx:21.0.1' def camerax_version = "1.0.2" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' @@ -53,4 +54,6 @@ dependencies { testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' -} \ No newline at end of file +} + +apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin diff --git a/app/src/androidTest/java/com/example/bump/MessageProcessorTest.kt b/app/src/androidTest/java/com/maenle/bump/MessageProcessorTest.kt similarity index 94% rename from app/src/androidTest/java/com/example/bump/MessageProcessorTest.kt rename to app/src/androidTest/java/com/maenle/bump/MessageProcessorTest.kt index 72f29d5..1c02a75 100644 --- a/app/src/androidTest/java/com/example/bump/MessageProcessorTest.kt +++ b/app/src/androidTest/java/com/maenle/bump/MessageProcessorTest.kt @@ -1,9 +1,9 @@ -package com.example.bump +package com.maenle.bump import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry -import com.example.bump.util.LocalData -import com.example.bump.util.MessageProcessor +import com.maenle.bump.util.LocalData +import com.maenle.bump.util.MessageProcessor import org.junit.Assert import org.junit.Test import org.junit.runner.RunWith diff --git a/app/src/androidTest/java/com/example/bump/RestCryptTest.kt b/app/src/androidTest/java/com/maenle/bump/RestCryptTest.kt similarity index 95% rename from app/src/androidTest/java/com/example/bump/RestCryptTest.kt rename to app/src/androidTest/java/com/maenle/bump/RestCryptTest.kt index 7b6f042..16eed15 100644 --- a/app/src/androidTest/java/com/example/bump/RestCryptTest.kt +++ b/app/src/androidTest/java/com/maenle/bump/RestCryptTest.kt @@ -1,10 +1,10 @@ -package com.example.bump +package com.maenle.bump import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.example.bump.util.Message -import com.example.bump.util.MessageProcessor -import com.example.bump.util.RestSingleton +import com.maenle.bump.util.Message +import com.maenle.bump.util.MessageProcessor +import com.maenle.bump.util.RestSingleton import org.json.JSONArray import org.json.JSONObject diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a4014c9..d225e69 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,8 +7,9 @@ + + + + + + + + + + + + + + + var message = getString(R.string.message_subscribed) + if (!task.isSuccessful) { + message = getString(R.string.message_subscribe_failed) + } + Toast.makeText(context, message, Toast.LENGTH_SHORT).show() + } + // [END subscribe_topics] } companion object { fun newInstance() = FirstFragment() + private val TOPIC = "Bump" } diff --git a/app/src/main/java/com/example/bump/ui/MainActivity.kt b/app/src/main/java/com/maenle/bump/ui/MainActivity.kt similarity index 98% rename from app/src/main/java/com/example/bump/ui/MainActivity.kt rename to app/src/main/java/com/maenle/bump/ui/MainActivity.kt index 285aa25..2442ccc 100644 --- a/app/src/main/java/com/example/bump/ui/MainActivity.kt +++ b/app/src/main/java/com/maenle/bump/ui/MainActivity.kt @@ -1,4 +1,4 @@ -package com.example.bump.ui +package com.maenle.bump.ui import android.os.Bundle import com.google.android.material.snackbar.Snackbar diff --git a/app/src/main/java/com/example/bump/ui/MainApplication.kt b/app/src/main/java/com/maenle/bump/ui/MainApplication.kt similarity index 87% rename from app/src/main/java/com/example/bump/ui/MainApplication.kt rename to app/src/main/java/com/maenle/bump/ui/MainApplication.kt index ca57ec9..2509d59 100644 --- a/app/src/main/java/com/example/bump/ui/MainApplication.kt +++ b/app/src/main/java/com/maenle/bump/ui/MainApplication.kt @@ -1,9 +1,9 @@ -package com.example.bump.ui +package com.maenle.bump.ui import android.app.Application import android.content.Context -import com.example.bump.util.BumpProcessor -import com.example.bump.util.RestSingleton +import com.maenle.bump.util.BumpProcessor +import com.maenle.bump.util.RestSingleton // Not object class. AndroidManifest.xml error happen. class MainApplication : Application() { diff --git a/app/src/main/java/com/example/bump/ui/SecondFragment.kt b/app/src/main/java/com/maenle/bump/ui/SecondFragment.kt similarity index 92% rename from app/src/main/java/com/example/bump/ui/SecondFragment.kt rename to app/src/main/java/com/maenle/bump/ui/SecondFragment.kt index 08770d2..1a3b006 100644 --- a/app/src/main/java/com/example/bump/ui/SecondFragment.kt +++ b/app/src/main/java/com/maenle/bump/ui/SecondFragment.kt @@ -1,11 +1,10 @@ -package com.example.bump.ui +package com.maenle.bump.ui import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.navigation.fragment.findNavController import com.maenle.bump.databinding.FragmentSecondBinding /** diff --git a/app/src/main/java/com/example/bump/util/BumpProcessor.kt b/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt similarity index 99% rename from app/src/main/java/com/example/bump/util/BumpProcessor.kt rename to app/src/main/java/com/maenle/bump/util/BumpProcessor.kt index 44be402..70b6bb9 100644 --- a/app/src/main/java/com/example/bump/util/BumpProcessor.kt +++ b/app/src/main/java/com/maenle/bump/util/BumpProcessor.kt @@ -1,4 +1,4 @@ -package com.example.bump.util +package com.maenle.bump.util import android.content.Context import android.os.Handler diff --git a/app/src/main/java/com/example/bump/util/CameraXViewModel.kt b/app/src/main/java/com/maenle/bump/util/CameraXViewModel.kt similarity index 98% rename from app/src/main/java/com/example/bump/util/CameraXViewModel.kt rename to app/src/main/java/com/maenle/bump/util/CameraXViewModel.kt index 29a4d0a..f46b9fa 100644 --- a/app/src/main/java/com/example/bump/util/CameraXViewModel.kt +++ b/app/src/main/java/com/maenle/bump/util/CameraXViewModel.kt @@ -1,4 +1,4 @@ -package com.example.bump.util +package com.maenle.bump.util import android.app.Application import android.util.Log diff --git a/app/src/main/java/com/example/bump/util/LocalData.kt b/app/src/main/java/com/maenle/bump/util/LocalData.kt similarity index 95% rename from app/src/main/java/com/example/bump/util/LocalData.kt rename to app/src/main/java/com/maenle/bump/util/LocalData.kt index 5b6aac6..ff052ad 100644 --- a/app/src/main/java/com/example/bump/util/LocalData.kt +++ b/app/src/main/java/com/maenle/bump/util/LocalData.kt @@ -1,4 +1,4 @@ -package com.example.bump.util +package com.maenle.bump.util import android.content.Context import android.content.Context.MODE_PRIVATE diff --git a/app/src/main/java/com/example/bump/util/Message.kt b/app/src/main/java/com/maenle/bump/util/Message.kt similarity index 83% rename from app/src/main/java/com/example/bump/util/Message.kt rename to app/src/main/java/com/maenle/bump/util/Message.kt index 5fe47b6..f23f0cd 100644 --- a/app/src/main/java/com/example/bump/util/Message.kt +++ b/app/src/main/java/com/maenle/bump/util/Message.kt @@ -1,8 +1,7 @@ -package com.example.bump.util +package com.maenle.bump.util import java.time.LocalDate import java.time.format.DateTimeFormatter -import java.util.* class Message(var sender: String, var data: String, timestamp: String) { val timestamp = LocalDate.parse(timestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS")) diff --git a/app/src/main/java/com/example/bump/util/MessageProcessor.kt b/app/src/main/java/com/maenle/bump/util/MessageProcessor.kt similarity index 98% rename from app/src/main/java/com/example/bump/util/MessageProcessor.kt rename to app/src/main/java/com/maenle/bump/util/MessageProcessor.kt index 7c406f9..8109a70 100644 --- a/app/src/main/java/com/example/bump/util/MessageProcessor.kt +++ b/app/src/main/java/com/maenle/bump/util/MessageProcessor.kt @@ -1,4 +1,4 @@ -package com.example.bump.util +package com.maenle.bump.util import java.util.Base64 import javax.crypto.spec.PBEKeySpec @@ -7,7 +7,7 @@ import com.macasaet.fernet.Key import com.macasaet.fernet.Token import com.macasaet.fernet.StringValidator import com.macasaet.fernet.Validator -import com.example.bump.ui.MainActivity +import com.maenle.bump.ui.MainActivity import java.math.BigInteger import java.security.SecureRandom import java.time.Duration diff --git a/app/src/main/java/com/example/bump/util/NotificationUtil.kt b/app/src/main/java/com/maenle/bump/util/NotificationUtil.kt similarity index 69% rename from app/src/main/java/com/example/bump/util/NotificationUtil.kt rename to app/src/main/java/com/maenle/bump/util/NotificationUtil.kt index f4bebf5..7929bf1 100644 --- a/app/src/main/java/com/example/bump/util/NotificationUtil.kt +++ b/app/src/main/java/com/maenle/bump/util/NotificationUtil.kt @@ -1,20 +1,19 @@ -package com.example.bump.util +package com.maenle.bump.util +import android.annotation.SuppressLint import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent import android.graphics.BitmapFactory import androidx.core.app.NotificationCompat -import com.example.bump.receiver.SnoozeReceiver -import com.example.bump.ui.MainActivity +import com.maenle.bump.ui.MainActivity import com.maenle.bump.R // Notification ID. private val NOTIFICATION_ID = 0 -private val REQUEST_CODE = 0 -private val FLAGS = 0 +@SuppressLint("WrongConstant") fun NotificationManager.sendNotification(messageBody: String, applicationContext: Context) { // Create the content intent for the notification, which launches // this activity @@ -33,17 +32,6 @@ fun NotificationManager.sendNotification(messageBody: String, applicationContext applicationContext.resources, R.drawable.ic_launcher_foreground ) - val bigPicStyle = NotificationCompat.BigPictureStyle() - .bigPicture(eggImage) - .bigLargeIcon(null) - - // TODO: Step 2.2 add snooze action - val snoozeIntent = Intent(applicationContext, SnoozeReceiver::class.java) - val snoozePendingIntent: PendingIntent = PendingIntent.getBroadcast( - applicationContext, - REQUEST_CODE, - snoozeIntent, - FLAGS) // TODO: Step 1.2 get an instance of NotificationCompat.Builder // Build the notification @@ -52,8 +40,6 @@ fun NotificationManager.sendNotification(messageBody: String, applicationContext applicationContext.getString(R.string.bump_notification_channel_id) ) - // TODO: Step 1.8 use the new 'breakfast' notification channel - // TODO: Step 1.3 set title, text and icon to builder .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentTitle(applicationContext @@ -65,16 +51,8 @@ fun NotificationManager.sendNotification(messageBody: String, applicationContext .setAutoCancel(true) // TODO: Step 2.1 add style to builder - .setStyle(bigPicStyle) .setLargeIcon(eggImage) - // TODO: Step 2.3 add snooze action - .addAction( - R.drawable.ic_launcher_foreground, - applicationContext.getString(R.string.snooze), - snoozePendingIntent - ) - // TODO: Step 2.5 set priority .setPriority(NotificationCompat.PRIORITY_HIGH) // TODO: Step 1.4 call notify diff --git a/app/src/main/java/com/example/bump/util/RestSingleton.kt b/app/src/main/java/com/maenle/bump/util/RestSingleton.kt similarity index 97% rename from app/src/main/java/com/example/bump/util/RestSingleton.kt rename to app/src/main/java/com/maenle/bump/util/RestSingleton.kt index 0425040..73f335a 100644 --- a/app/src/main/java/com/example/bump/util/RestSingleton.kt +++ b/app/src/main/java/com/maenle/bump/util/RestSingleton.kt @@ -1,11 +1,11 @@ -package com.example.bump.util +package com.maenle.bump.util import android.content.Context import android.util.Log import com.android.volley.Request import com.android.volley.RequestQueue import com.android.volley.toolbox.* -import com.example.bump.ui.MainActivity +import com.maenle.bump.ui.MainActivity import org.json.JSONArray import org.json.JSONObject diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f2ee8e5..133a703 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="com.example.bump.ui.MainActivity"> + tools:context="com.maenle.bump.ui.MainActivity"> + tools:context="com.maenle.bump.ui.SecondFragment">