Før RecyclerView i NestedScrollView (Ytelse)
This commit is contained in:
parent
f4cd9603fa
commit
864820212f
3 changed files with 137 additions and 5 deletions
|
|
@ -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"
|
||||
}
|
||||
|
|
@ -71,4 +71,7 @@ dependencies {
|
|||
|
||||
// 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")
|
||||
}
|
||||
|
|
@ -49,6 +49,15 @@
|
|||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
|
||||
<!-- NYTT: Registrering av Firebase Messaging Service -->
|
||||
<service
|
||||
android:name=".MyFirebaseMessagingService"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
<action android:name="com.google.firebase.MESSAGING_EVENT" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="com.kbs.kbsintranett.fileprovider"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,120 @@
|
|||
package com.kbs.kbsintranett;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import com.google.firebase.messaging.FirebaseMessagingService;
|
||||
import com.google.firebase.messaging.RemoteMessage;
|
||||
|
||||
public class MyFirebaseMessagingService extends FirebaseMessagingService {
|
||||
|
||||
private static final String TAG = "FCMService";
|
||||
private static final String CHANNEL_ID = "kbs_calendar_channel"; // Samme kanal som før
|
||||
|
||||
/**
|
||||
* Kalles når en ny token genereres (f.eks. ved ny installasjon).
|
||||
* Denne tokenen MÅ sendes til WordPress-backend slik at serveren vet hvem den skal sende til.
|
||||
*/
|
||||
@Override
|
||||
public void onNewToken(@NonNull String token) {
|
||||
super.onNewToken(token);
|
||||
Log.d(TAG, "Ny FCM Token: " + token);
|
||||
|
||||
// TODO: Send denne tokenen til din WordPress-backend via AuthRepository eller RetrofitClient.
|
||||
// F.eks: AuthRepository.updateDeviceToken(token);
|
||||
// Vi lagrer den midlertidig i UserManager eller SharedPreferences hvis brukeren ikke er logget inn enda.
|
||||
}
|
||||
|
||||
/**
|
||||
* Kalles når en melding mottas mens appen er i forgrunnen,
|
||||
* ELLER hvis det er en "Data Message" (som er det vi bør bruke for bakgrunnsoppdateringer).
|
||||
*/
|
||||
@Override
|
||||
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
|
||||
super.onMessageReceived(remoteMessage);
|
||||
|
||||
Log.d(TAG, "Melding mottatt fra: " + remoteMessage.getFrom());
|
||||
|
||||
// Sjekk om meldingen inneholder data (payload)
|
||||
if (remoteMessage.getData().size() > 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue