Tugas Mobile Programming - Praktikum 8 | Teknik Informatika UNPI
NAMA : MUHAMMAD MUSLIM ABDUL JABBAAR
NIM : 217200035
PRODI : TEKNIK INFORMATIKA
MATKUL : MOBILE PROGRAMMING 1
Push Notification
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/notifikasi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifikasi"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
- activity_after_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".afterNotification">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Notifikasi Berhasil!"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
- MainActivity.kt
package com.opendebugger62.pertemuan10
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.RemoteViews
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
private val channelId = "i.apps.notifications"
private val description = "Test notification"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<View>(R.id.notifikasi)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
btn.setOnClickListener {
val intent = Intent(this, afterNotification::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val contentView = RemoteViews(packageName, R.layout.activity_after_notification)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)
builder = Notification.Builder(this, channelId)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
} else {
builder = Notification.Builder(this)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
}
notificationManager.notify(1234, builder.build())
}
}
}
- afterNotification.kt
package com.opendebugger62.pertemuan10
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class afterNotification : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_after_notification)
}
}
Komentar
Posting Komentar