Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'com.google.gms.google-services'
}

android {
Expand Down Expand Up @@ -47,6 +48,8 @@ dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.firebase:firebase-messaging:21.0.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.uzlov.moviefind">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Expand All @@ -24,7 +25,7 @@
android:value="@string/google_maps_key" />

<activity
android:name=".HostActivity"
android:name=".activities.HostActivity"
android:label="@string/app_name"
android:theme="@style/Theme.MovieFind.NoActionBar">
<intent-filter>
Expand All @@ -36,12 +37,20 @@
<activity
android:name=".activities.ShareActivity"
android:label="@string/share_friends"
android:theme="@style/Theme.MovieFind.NoActionBar"></activity>
android:theme="@style/Theme.MovieFind.NoActionBar" />

<service
android:name=".services.SampleService"
android:enabled="true"
android:exported="true" />
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uzlov.moviefind
package com.uzlov.moviefind.activities

import com.uzlov.moviefind.services.SampleService
import android.content.BroadcastReceiver
Expand All @@ -11,6 +11,7 @@ import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.uzlov.moviefind.R
import com.uzlov.moviefind.databinding.ActivityMainBinding
import com.uzlov.moviefind.fragments.*
import com.uzlov.moviefind.services.NetworkListener
Expand Down Expand Up @@ -42,7 +43,7 @@ class HostActivity : AppCompatActivity() {
setActiveFragment(homeFragment)
true
}
R.id.menu_favorite-> {
R.id.menu_favorite -> {
setActiveFragment(popularFilmsFragment)
true
}
Expand All @@ -53,7 +54,11 @@ class HostActivity : AppCompatActivity() {
else -> false
}
}

startNetworkListener()

val intent = Intent(this, SampleService::class.java)
startService(intent)
}

private fun startNetworkListener(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.uzlov.moviefind.R
import com.uzlov.moviefind.databinding.FragmentMapsBinding
import com.uzlov.moviefind.repository.RepositoryRemoteImpl
import com.uzlov.moviefind.viewmodels.FilmsViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand Down
60 changes: 27 additions & 33 deletions app/src/main/java/com/uzlov/moviefind/services/SampleService.kt
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
package com.uzlov.moviefind.services

import android.app.*
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.uzlov.moviefind.HostActivity
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.uzlov.moviefind.R
import com.uzlov.moviefind.activities.HostActivity

class SampleService : Service() {
private val CHANNEL_ID: String = "ForegroundServiceChannel"
class SampleService : FirebaseMessagingService() {
private val NOTIFICATION_ID : Int = 1
private val CHANNEL_ID by lazy {resources.getString(R.string.default_notification_channel_id)}

companion object{
val VALUE_KEY: String = "sample_key"
val ACTION = "ACTION_SAMPLE_SERVICE"
const val VALUE_KEY: String = "sample_key"
}

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
createNotificationChannel()
override fun onNewToken(p0: String) {
super.onNewToken(p0)
}

override fun onMessageReceived(message: RemoteMessage) {

val manager = createNotificationChannel()
val notificationIntent = Intent(this, HostActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0)
val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, 0
)
val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.funny_string))
.setSmallIcon(R.drawable.ic_baseline_favorite_border_24)
.setContentText(message.notification?.body ?: "Empty body")
.setContentIntent(pendingIntent)
.build()
startForeground(1, notification)

Thread {
for (i in 0..5){
Thread.sleep(1000)
val data = Intent().apply {
putExtra(VALUE_KEY, (i % 2 == 0).toString())
action = ACTION
}
sendBroadcast(data)
if (i == 4) stopSelf()
}
}.start()

return START_STICKY

manager?.notify(NOTIFICATION_ID, notificationBuilder.build())
}

private fun createNotificationChannel() {
private fun createNotificationChannel() : NotificationManager? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
CHANNEL_ID,
Expand All @@ -53,10 +49,8 @@ class SampleService : Service() {
)
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(serviceChannel)
return manager
}
}

override fun onBind(p0: Intent?): IBinder? {
return null
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:layout_height="match_parent"
android:orientation="vertical"
tools:viewBindingIgnore="false"
tools:context=".HostActivity">
tools:context=".activities.HostActivity">



Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-night/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="string_from_moviefind">Это уведомление из MovieFind</string>
<string name="populate_label">Популярные</string>
<string name="share_friends">Поделись с друзьями</string>
<string name="default_notification_channel_id">channel_fcm</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<dimen name="corner_radius_search_et">32dp</dimen>
<dimen name="size_warning_picture">100dp</dimen>
<dimen name="fonts_size_phone">18sp</dimen>
<dimen name="height_map">300dp</dimen>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@
</string>
<string name="attachment_summary_off">Only download attachments when manually requested</string>
<string name="share_friends">Поделись с друзьями</string>
<string name="default_notification_channel_id">channel_fcm</string>
</resources>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:4.2.0-alpha09"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down