2

I have checked many answers, but they didn't work. I just want to create simple push notification app. First I created for single device when I made an apk and tried to send notification on multiple devices it starts crashing.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    String TAG ="MALIK";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // ...
        sendNotification(remoteMessage.getNotification().getBody());
        // TODO(developer): Handle FCM messages here.
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            if (/* Check if data needs to be processed by long-running job */ true) {
                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.

            } else {
                // Handle message within 10 seconds

            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(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.
    }

    @Override
    public void onNewToken(String token) {

        Log.d(TAG, "Refreshed token: " + token);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.

    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

3

The error can be summarized to ClassNotFoundException.

It means the code called a class that is not in the class path. The issue can be because:

  • The library Firebase-iid needs to be added.

    • // use compile instead of implementation if it's unknown
      implementation "com.google.firebase:firebase-iid:+" // 18.0.0 or 20.0.0
      
  • The library firebase-messaging is added (and adds this class too), but it's very old or very new, so it's not compatible with your code.

    • Make sure you add firebase messaging to the project:
    // use compile instead of implementation if it's unknown
    implementation "com.google.firebase:firebase-messaging:+" // 18.0.0 or 20.0.0
    
    • If your code is old (copied from an ancient post blog), add a lower firebase-messaging library version. Else if your code is new and your firebase-messaging is old, try upgrading the library version.

More info on firebase release notes.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
1

In my case this has issue fixed by adding this dependency:

implementation "androidx.legacy:legacy-support-core-utils:1.0.0"

I found this solution accidentally. If it doesn't work give me feedback to edit/delete my post.

beigirad
  • 4,986
  • 2
  • 29
  • 52