Hi guys I have this problem, I have programmed an App send notifications via Firebase Cloud Messaging below the code:
package com.app.name.Service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.fourapper.forpaper.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import java.util.Random;
public class MyFirebaseService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().isEmpty()){
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}else{
showNotification1(remoteMessage.getData());
}
}
private void showNotification1(Map<String, String> data) {
String title = data.get("title").toString();
String body = data.get("body").toString();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.app.name.Test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Channel");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{ 0, 1000, 500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_money_bag)
.setVibrate(new long[]{500, 500})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.app.name.Test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Channel");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{ 0, 1000, 500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_money_bag)
.setVibrate(new long[]{500, 500})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
Log.d("TOKENFIREBASE", s);
}
}
everything works only that I have encountered this problem, since I have scheduled the sending of a daily notification, I noticed that if I don't access the app in the day the notification is not sent to me, instead if I access the app in the day the notification it comes to me, I can't understand where the problem is, if you can help me thanks