0

So, I'm developing an Android app and it receives notifications that come from Firebase. To receive them, there is no problem, but the question is that I only can customize the way they are shown up in the user's device while the app is open and it's in the main screen. Therefore, when the app is closed the notifications come with no icon, no sound and neither with a vibration.

You have to understand that however I've already changed what I wanted in the NotificationCompat class, these configurations just aren't apply in the app while it is closed. See the code below.

So, I hope that I've been understandable and if someone there could say what is happening, I'd be really grateful.

Class to receive the Notifications

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);
 }

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

If you want to customize notification, you shouldnt use firebase console.
Look here for more information

You should also add below code for image:

Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
notificationBuilder.setLargeIcon(largeIcon)

Second thing is about notification sound, if you want custom sound, use this:

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
notificationBuilder.setSound(uri);
mac229
  • 4,319
  • 5
  • 18
  • 24
  • Thanks for answering man, but after reading what you've passed I still don't understand how I can pass icons or sounds in the notification and treat them in the app while it is in background, cause I'm using the Firebase Cloud Functions to send the notifications and in the app I don't receive the icons as I've passed before. – Topazzio App Aug 15 '17 at 11:33