Search code examples
androidfirebasenotify

Why will my android notification not send?


I am sending DATA payload from Postman, so the notification should be processed in onMessageReceived regardless of whether the app is in foreground or background.

According my logs and testing, onMessageReceived is fired, but the notifications are not showing (doesn't depend if the app is in foreground or background).

in manifest I have:

<service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service> <!-- [END firebase_iid_service] -->
    <service
        android:name=".MyFirebaseInstanceIDService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

and here is MyFirebaseMessagingService.java:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String title = "";
        if (Objects.requireNonNull(remoteMessage.getData()).get("title") != null)
            title = remoteMessage.getData().get("title");

        String message = "";
        if (Objects.requireNonNull(remoteMessage.getData()).get("message") != null)
            message = remoteMessage.getData().get("message");

        Log.e("notification",title);
        sendNotification(title, message);
    }

    private void sendNotification(String title, String body) {

        Intent i = new Intent(this, Novinky.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pi = PendingIntent.getActivity(this,
                0, // Request code
                i,
                PendingIntent.FLAG_ONE_SHOT);

        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
            getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.mipmap.ic_notify)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setSound(sound)
                .setContentIntent(pi);

        NotificationManager manager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        assert manager != null;
        manager.notify(0, builder.build());
    }
}

So I have Log.e("notification",title); there, and it is executed properly, but the notify is not received.

Before I send also a notification+data via Postman, that worked, but that is only an automatic notification not handled by app, so now I want to send only DATA payload and handle it in the app, but for some reason it doesn't work.


Solution

  • You need to make sure you have created your notification channel in your app. A good way to do this is as follows:

    public class App extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            createNotificationChannel();
        }
    
        private void createNotificationChannel() {
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel nChan = new NotificationChannel(
                    getString(R.string.default_notification_channel_id),
                    "My_Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
                );
    
                NotificationManager manager = getSystemService(NotificationManager.class);
                manager.createNotificationChannel(nChan);
            }
        }
    }
    

    Creating your notification channel in the onCreate() of your app is a good idea as you don't have to worry about it for the rest of the application life time.