0

Whenever I use the Firebase Console to send a notification. The notification doesn't show up on the device, but the activity changes to another activity or some action occurs on the application.

When sending the notification from the console, I add the topic as well as the current app ID and the notification ID as "MyNotifications".

I have followed instructions online and created a class called MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
    }

    public void showNotification(String title, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MyNotifications")
                .setContentTitle(title)
                .setContentText(message);

        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        manager.notify(999, builder.build());
    }
}

My MainActivity consists of this code in OnCreate:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NotificationChannel channel =
                    new NotificationChannel("MyNotifications", "MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }

        FirebaseMessaging.getInstance().subscribeToTopic("general")
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        String msg = "Successful!";
                        if (!task.isSuccessful()) {
                            msg = "Failed";
                        }
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });

I've also added this in my AndroidManifest:

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
MrPool
  • 370
  • 9
  • 27
  • Didi you tried using `NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);` ? – Nick Bapu May 22 '19 at 11:53
  • Just tried that, the app closes and returns back to the emulator home page – MrPool May 22 '19 at 11:58

1 Answers1

0

So since API Level 21 (Lollipop), it is mandatory to place a small icon in the notifications. That is why the app was crashing and returning to home. So the only changes made to the code here were:

                .setSmallIcon(R.drawable.fui_ic_twitter_bird_white_24dp);

For more on this topic, check this question out.

MrPool
  • 370
  • 9
  • 27