praktik 1
This commit is contained in:
parent
08c24bdfdd
commit
82212b9cc7
@ -57,6 +57,9 @@ dependencies {
|
|||||||
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
|
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
|
||||||
debugImplementation(libs.androidx.compose.ui.tooling)
|
debugImplementation(libs.androidx.compose.ui.tooling)
|
||||||
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
||||||
|
implementation("androidx.compose.material3:material3:1.1.1")
|
||||||
|
|
||||||
//praktikum 1
|
//praktikum 1
|
||||||
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
||||||
implementation("androidx.compose.material3:material3:1.1.1")
|
implementation("androidx.compose.material3:material3:1.1.1")
|
||||||
|
|||||||
@ -3,16 +3,13 @@ package id.ac.ubharajaya.panicbutton
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.unit.dp
|
||||||
import id.ac.ubharajaya.panicbutton.ui.theme.PanicButtonTheme
|
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
@ -21,32 +18,75 @@ import okhttp3.RequestBody
|
|||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
|
||||||
setContent {
|
setContent {
|
||||||
PanicButtonTheme {
|
MyApp()
|
||||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
||||||
Greeting(
|
|
||||||
name = "Android",
|
|
||||||
modifier = Modifier.padding(innerPadding)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun Greeting(name: String, modifier: Modifier = Modifier) {
|
|
||||||
Text(
|
|
||||||
text = "Hello $name!",
|
|
||||||
modifier = modifier
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Preview(showBackground = true)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun GreetingPreview() {
|
fun MyApp() {
|
||||||
PanicButtonTheme {
|
// State untuk menampilkan hasil request
|
||||||
Greeting("Android")
|
var message by remember { mutableStateOf("Klik tombol untuk mengirim notifikasi") }
|
||||||
|
|
||||||
|
|
||||||
|
// UI
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(16.dp),
|
||||||
|
verticalArrangement = Arrangement.Center,
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally
|
||||||
|
) {
|
||||||
|
Text(text = message, Modifier.padding(bottom = 16.dp))
|
||||||
|
Button(onClick = {
|
||||||
|
// Kirim HTTP request saat tombol ditekan
|
||||||
|
sendNotification { response ->
|
||||||
|
message = response
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
Text(text = "Kirim Alert")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Fungsi untuk mengirimkan HTTP request
|
||||||
|
fun sendNotification(onResult: (String) -> Unit) {
|
||||||
|
val client = OkHttpClient()
|
||||||
|
val url = "https://ntfy.ubharajaya.ac.id/panic-button" // Ganti <your-topic> dengan topik Anda
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
val requestBody = RequestBody.create(
|
||||||
|
"text/plain".toMediaType(), // Mengirim plain text
|
||||||
|
"Notifikasi dari Rakha Adi Saputro 202310715083!" // Pesan yang akan tampil
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
val request = Request.Builder()
|
||||||
|
.url(url)
|
||||||
|
.addHeader("Title", "Alert")
|
||||||
|
.addHeader("Priority", "urgent")
|
||||||
|
.addHeader("Tags", "alert warning,rotating_light")
|
||||||
|
.post(requestBody)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Eksekusi request di thread terpisah
|
||||||
|
Thread {
|
||||||
|
try {
|
||||||
|
val response = client.newCall(request).execute()
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
onResult("Notifikasi berhasil dikirim!")
|
||||||
|
} else {
|
||||||
|
onResult("Gagal mengirim notifikasi: ${response.code}")
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
onResult("Error: ${e.message}")
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user