Archived
Replaced recurring background fetch with Firebase solution including broadcast function
This commit is contained in:
+6
-2
@@ -9,8 +9,8 @@ android {
|
|||||||
applicationId "com.denizd.substitutionplan"
|
applicationId "com.denizd.substitutionplan"
|
||||||
minSdkVersion 21
|
minSdkVersion 21
|
||||||
targetSdkVersion 28
|
targetSdkVersion 28
|
||||||
versionCode 22
|
versionCode 23
|
||||||
versionName "2.1.5"
|
versionName "2.2.0"
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
@@ -35,6 +35,8 @@ dependencies {
|
|||||||
implementation 'com.google.android.material:material:1.1.0-alpha09'
|
implementation 'com.google.android.material:material:1.1.0-alpha09'
|
||||||
implementation 'com.android.support:customtabs:28.0.0'
|
implementation 'com.android.support:customtabs:28.0.0'
|
||||||
|
|
||||||
|
implementation 'com.google.firebase:firebase-messaging:19.0.1'
|
||||||
|
|
||||||
// implementation 'androidx.core:core-ktx:1.0.2'
|
// implementation 'androidx.core:core-ktx:1.0.2'
|
||||||
|
|
||||||
implementation 'com.jaredrummler:android-device-names:1.1.8'
|
implementation 'com.jaredrummler:android-device-names:1.1.8'
|
||||||
@@ -55,3 +57,5 @@ dependencies {
|
|||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apply plugin: 'com.google.gms.google-services'
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"project_info": {
|
||||||
|
"project_number": "90133755549",
|
||||||
|
"firebase_url": "https://avh-plan.firebaseio.com",
|
||||||
|
"project_id": "avh-plan",
|
||||||
|
"storage_bucket": "avh-plan.appspot.com"
|
||||||
|
},
|
||||||
|
"client": [
|
||||||
|
{
|
||||||
|
"client_info": {
|
||||||
|
"mobilesdk_app_id": "1:90133755549:android:8ac337c5a1500393",
|
||||||
|
"android_client_info": {
|
||||||
|
"package_name": "com.denizd.substitutionplan"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"oauth_client": [
|
||||||
|
{
|
||||||
|
"client_id": "90133755549-vs0k1khnuqc7efq2o1kfuikmoaam1j07.apps.googleusercontent.com",
|
||||||
|
"client_type": 3
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"api_key": [
|
||||||
|
{
|
||||||
|
"current_key": "AIzaSyC0fZvAhNlKfZ9-iU3lZ0Te8C4s8cT9F38"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"services": {
|
||||||
|
"appinvite_service": {
|
||||||
|
"other_platform_oauth_client": [
|
||||||
|
{
|
||||||
|
"client_id": "90133755549-vs0k1khnuqc7efq2o1kfuikmoaam1j07.apps.googleusercontent.com",
|
||||||
|
"client_type": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"client_id": "90133755549-74enm9fefgrj5v5009c11194qi71m09f.apps.googleusercontent.com",
|
||||||
|
"client_type": 2,
|
||||||
|
"ios_info": {
|
||||||
|
"bundle_id": "denizd.AvH-Plan"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"configuration_version": "1"
|
||||||
|
}
|
||||||
@@ -37,9 +37,20 @@
|
|||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<service
|
<service
|
||||||
android:name=".NotificationService"
|
android:name=".FBNotificationService"
|
||||||
android:permission="android.permission.BIND_JOB_SERVICE"
|
android:exported="false">
|
||||||
android:required="true"/>
|
<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="@drawable/ic_avhlogo" />
|
||||||
|
<meta-data
|
||||||
|
android:name="com.google.firebase.messaging.default_notification_color"
|
||||||
|
android:resource="@color/colorAccent" />
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.denizd.substitutionplan
|
||||||
|
|
||||||
|
import com.google.firebase.messaging.FirebaseMessagingService
|
||||||
|
import com.google.firebase.messaging.RemoteMessage
|
||||||
|
|
||||||
|
class FBNotificationService : FirebaseMessagingService() {
|
||||||
|
|
||||||
|
override fun onMessageReceived(p0: RemoteMessage?) {
|
||||||
|
DataFetcher(true, true, true, this, application, null).execute()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,8 +9,10 @@ import android.content.SharedPreferences
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
|
import android.util.Log
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
@@ -22,6 +24,8 @@ import androidx.recyclerview.widget.RecyclerView
|
|||||||
import com.google.android.material.appbar.AppBarLayout
|
import com.google.android.material.appbar.AppBarLayout
|
||||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
import com.google.firebase.FirebaseApp
|
||||||
|
import com.google.firebase.messaging.FirebaseMessaging
|
||||||
import com.jaredrummler.android.device.DeviceName
|
import com.jaredrummler.android.device.DeviceName
|
||||||
import java.lang.IllegalArgumentException
|
import java.lang.IllegalArgumentException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -51,6 +55,15 @@ class Main : AppCompatActivity(R.layout.app_bar_main) {
|
|||||||
edit.putInt("launchDev", prefs.getInt("launchDev", 0) + 1)
|
edit.putInt("launchDev", prefs.getInt("launchDev", 0) + 1)
|
||||||
edit.apply()
|
edit.apply()
|
||||||
|
|
||||||
|
FirebaseApp.initializeApp(context)
|
||||||
|
|
||||||
|
if (prefs.getBoolean("notif", true)) {
|
||||||
|
FirebaseMessaging.getInstance().subscribeToTopic("substitutions-android")
|
||||||
|
} else {
|
||||||
|
FirebaseMessaging.getInstance().unsubscribeFromTopic("substitutions-android")
|
||||||
|
}
|
||||||
|
FirebaseMessaging.getInstance().subscribeToTopic("substitutions-broadcast")
|
||||||
|
|
||||||
val appbarlayout = findViewById<AppBarLayout>(R.id.appbarlayout)
|
val appbarlayout = findViewById<AppBarLayout>(R.id.appbarlayout)
|
||||||
val toolbarTxt = findViewById<TextView>(R.id.toolbarTxt)
|
val toolbarTxt = findViewById<TextView>(R.id.toolbarTxt)
|
||||||
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)
|
val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav)
|
||||||
@@ -209,10 +222,6 @@ class Main : AppCompatActivity(R.layout.app_bar_main) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefs.getBoolean("notif", true)) {
|
|
||||||
notificationJob()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,15 +249,4 @@ class Main : AppCompatActivity(R.layout.app_bar_main) {
|
|||||||
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit()
|
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notificationJob() {
|
|
||||||
val componentName = ComponentName(this, NotificationService::class.java)
|
|
||||||
val info = JobInfo.Builder(42, componentName)
|
|
||||||
.setRequiresCharging(false)
|
|
||||||
.setPersisted(true)
|
|
||||||
.setPeriodic(900000)
|
|
||||||
.build()
|
|
||||||
val scheduler = getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
|
|
||||||
scheduler.schedule(info)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package com.denizd.substitutionplan
|
|
||||||
|
|
||||||
import android.app.job.JobParameters
|
|
||||||
import android.app.job.JobService
|
|
||||||
import android.content.Context
|
|
||||||
import android.net.ConnectivityManager
|
|
||||||
import android.preference.PreferenceManager
|
|
||||||
|
|
||||||
class NotificationService : JobService() {
|
|
||||||
|
|
||||||
private var jobCancelled = false
|
|
||||||
private val context = this
|
|
||||||
|
|
||||||
override fun onStartJob(params: JobParameters): Boolean {
|
|
||||||
doBackgroundWork(params)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStopJob(params: JobParameters?): Boolean {
|
|
||||||
jobCancelled = true
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun doBackgroundWork(params: JobParameters) {
|
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
|
||||||
val edit = prefs.edit()
|
|
||||||
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
||||||
val networkStatus = connectivityManager.activeNetworkInfo
|
|
||||||
|
|
||||||
Thread(Runnable {
|
|
||||||
if (!prefs.getBoolean("notif", false) || jobCancelled || networkStatus == null) {
|
|
||||||
jobFinished(params, false)
|
|
||||||
return@Runnable
|
|
||||||
} else {
|
|
||||||
edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
|
|
||||||
try {
|
|
||||||
DataFetcher(true, true, true, context, application, null).execute()
|
|
||||||
} catch (ignored: Exception) {}
|
|
||||||
|
|
||||||
jobFinished(params, false)
|
|
||||||
}
|
|
||||||
}).start()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,6 +24,7 @@ import androidx.recyclerview.widget.RecyclerView
|
|||||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||||
import com.google.android.material.button.MaterialButton
|
import com.google.android.material.button.MaterialButton
|
||||||
import com.google.android.material.textfield.TextInputEditText
|
import com.google.android.material.textfield.TextInputEditText
|
||||||
|
import com.google.firebase.messaging.FirebaseMessaging
|
||||||
import com.jaredrummler.android.device.DeviceName
|
import com.jaredrummler.android.device.DeviceName
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
|
||||||
@@ -231,6 +232,11 @@ class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListen
|
|||||||
}
|
}
|
||||||
R.id.switchNotifications -> {
|
R.id.switchNotifications -> {
|
||||||
edit.putBoolean("notif", isChecked)
|
edit.putBoolean("notif", isChecked)
|
||||||
|
if (isChecked) {
|
||||||
|
FirebaseMessaging.getInstance().subscribeToTopic("substitutions-android")
|
||||||
|
} else {
|
||||||
|
FirebaseMessaging.getInstance().unsubscribeFromTopic("substitutions-android")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
R.id.switchDefaultPlan -> {
|
R.id.switchDefaultPlan -> {
|
||||||
edit.putBoolean("defaultPersonalised", isChecked)
|
edit.putBoolean("defaultPersonalised", isChecked)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ buildscript {
|
|||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:3.5.0-beta02'
|
classpath 'com.android.tools.build:gradle:3.5.0-beta02'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
classpath 'com.google.gms:google-services:4.3.0'
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
// in the individual module build.gradle files
|
// in the individual module build.gradle files
|
||||||
|
|||||||
Reference in New Issue
Block a user