I am developing an app that receives notifications from firebase, I am able to receive notifications quietly when the application is open, everything I instantiated in NotificationCompat works, but when the application is in the background nothing works, the message is displayed with title and content, but the icon and sounds are not called. Someone could help me.
Class to receive the message
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
notifyuer(remoteMessage.getFrom(), remoteMessage.getNotification().getBody());
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
public void notifyuer(String from, String notification){
MyNotificationManager myNotificationManager = new MyNotificationManager(getApplicationContext());
myNotificationManager.showNotificacao(from,notification, new Intent(getApplicationContext(),MainActivity.class));
}
Custom Notification Class
public class MyNotificationManager {
private Context context;
public MyNotificationManager(Context context){
this.context = context;
}
public void showNotificacao(String from, String notification, Intent intent){
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
//long[] vibrar = {150,400,150,800};
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
Notification mNotification = notificationBuilder
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.logocinza64)
.setContentTitle("S.I.C.C.")
.setContentText(notification)
.setAutoCancel(true)
.setVibrate(new long[]{ 100, 250, 100, 500, 800})
.build();
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
try{
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone toque = RingtoneManager.getRingtone(context,defaultSoundUri);
toque.play();
}catch (Exception e){
}
notificationManager.notify(0 /* ID of notification */, mNotification);
}
}