Skip to content

Commit

Permalink
added firebase messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzcetinbas committed May 15, 2023
1 parent 7a8eb03 commit 2265859
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 9 deletions.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'

implementation platform('com.google.firebase:firebase-bom:32.0.0')
implementation 'com.google.firebase:firebase-messaging:23.1.2'

}
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:allowBackup="true"
Expand All @@ -23,6 +25,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name="MyFirebaseMessagingService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import com.example.hw13_weatherapp.model.data.WeatherResponse
interface WeatherPropertyDao {

@Query("SELECT * FROM weather_property")
suspend fun getAll(): WeatherResponse
fun getAll(): WeatherResponse

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(weatherResponse: WeatherResponse)
fun insert(weatherResponse: WeatherResponse)

@Query("DELETE FROM weather_property")
suspend fun deleteAllItem()
fun deleteAllItem()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
}

override fun onNewToken(token: String) {
super.onNewToken(token)
}
}
71 changes: 69 additions & 2 deletions app/src/main/java/com/example/hw13_weatherapp/view/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,81 @@
package com.example.hw13_weatherapp.view

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.example.hw13_weatherapp.R
import com.example.hw13_weatherapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)



createNotificationChannel()

binding.buttonNotification.setOnClickListener {
val notification = NotificationCompat.Builder(this, "NOTI_CHANNEL_ID")
.setSmallIcon(R.drawable.baseline_message_24)
.setContentTitle("UYGULAMAYI ACINIZ")
.setContentText("Uygulamayi acmak icin tiklayiniz !!!")
.setContentIntent(getPendingIntent())
.setPriority(NotificationCompat.COLOR_DEFAULT)
.build()

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED
) {
return@setOnClickListener
}

NotificationManagerCompat.from(this).notify(1, notification)
}
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

val name = "Notification Title"
val descricpitonText = "Notification Description"
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel("NOTI_CHANNEL_ID", name, importance).apply {
description = descricpitonText
}

val notificationManager: NotificationManager =
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}

setContentView(R.layout.activity_main)
private fun getPendingIntent(): PendingIntent {
val intent = Intent(
this,
MainActivity::class.java
)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
return PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_IMMUTABLE
)
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_message_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#EA0C0C" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z"/>
</vector>
13 changes: 10 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#1E1E1E"
android:orientation="vertical"
tools:context=".view.MainActivity">


<Button
android:id="@+id/buttonNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Notification !"
android:layout_gravity="center"
/>

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
/>
app:navGraph="@navigation/nav_graph" />

</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/layout/current_day_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_weather_icon" />


<ImageView
android:id="@+id/iv_wind_speed_icon"
android:layout_width="35dp"
Expand Down Expand Up @@ -109,6 +110,7 @@
app:layout_constraintTop_toTopOf="@id/iv_wind_direction_icon"
app:layout_constraintVertical_bias="0.363" />


<ImageView
android:id="@+id/iv_wind_direction_icon"
android:layout_width="23dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HW13WeatherApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.HW13WeatherApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down

0 comments on commit 2265859

Please sign in to comment.