diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 74369ec..196ce47 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -12,8 +12,8 @@ android { applicationId = "com.kbs.kbsintranett" minSdk = 28 targetSdk = 34 - versionCode = 3 - versionName = "1.4" + versionCode = 4 + versionName = "1.5.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } @@ -65,10 +65,13 @@ dependencies { // Swipe Refresh Layout implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0") - + // NY LINJE: Firebase BOM (Bill of Materials) styrer versjoner implementation(platform("com.google.firebase:firebase-bom:33.1.2")) - + // NY LINJE: (Valgfritt, men lurt for statistikk) implementation("com.google.firebase:firebase-analytics") -} + + // NYTT: Firebase Cloud Messaging lagt til her + implementation("com.google.firebase:firebase-messaging") +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1521411..78a2188 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -49,6 +49,15 @@ android:enabled="true" android:exported="false" /> + + + + + + + 0) { + Log.d(TAG, "Melding data payload: " + remoteMessage.getData()); + // Her kan du trigge en oppdatering av kalenderen i bakgrunnen uten å vise varsel, + // eller vise et varsel basert på dataene. + + String title = remoteMessage.getData().get("title"); + String body = remoteMessage.getData().get("body"); + + if (title != null && body != null) { + showNotification(title, body); + } + } + + // Sjekk om meldingen er en ren varslingsmelding (Notification payload) + if (remoteMessage.getNotification() != null) { + Log.d(TAG, "Melding varsel body: " + remoteMessage.getNotification().getBody()); + showNotification( + remoteMessage.getNotification().getTitle(), + remoteMessage.getNotification().getBody() + ); + } + } + + private void showNotification(String title, String message) { + // Gjenbruk logikk for kanalopprettelse (sikkerhetsnett) + createNotificationChannel(); + + // Sjekk rettigheter for Android 13+ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + return; + } + } + + Intent tapIntent = new Intent(this, MainActivity.class); + tapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + PendingIntent pendingIntent = PendingIntent.getActivity( + this, + 0, + tapIntent, + PendingIntent.FLAG_IMMUTABLE + ); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_stat_kbs) + .setColor(ContextCompat.getColor(this, R.color.kbs_logo_blue)) + .setContentTitle(title) + .setContentText(message) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setContentIntent(pendingIntent) + .setAutoCancel(true); + + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); + // Bruk systemtid som ID for å unngå at varsler overskriver hverandre, eller en fast ID hvis ønskelig + notificationManager.notify((int) System.currentTimeMillis(), builder.build()); + } + + private void createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel( + CHANNEL_ID, + "KBS Kalendervarsler", + NotificationManager.IMPORTANCE_HIGH + ); + channel.setDescription("Varsler fra KBS Intranett"); + NotificationManager manager = getSystemService(NotificationManager.class); + if (manager != null) { + manager.createNotificationChannel(channel); + } + } + } +} \ No newline at end of file