5

I have trouble but don't understand why it's happened, I have two type notification in app, ChatNotification and PostNotification, after add new post, i get this error:

give me error in line if (notificationType.equals("PostNotification")){

E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
    Process: com.example.chatbox, PID: 24011
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.example.chatbox.notifications.FirebaseMessaging.onMessageReceived(FirebaseMessaging.java:58)
        at com.google.firebase.messaging.FirebaseMessagingService.zzc(com.google.firebase:firebase-messaging@@20.2.0:80)
        at com.google.firebase.messaging.zzh.run(com.google.firebase:firebase-messaging@@20.2.0:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.1.1:6)
        at java.lang.Thread.run(Thread.java:764)

public class FirebaseMessaging extends FirebaseMessagingService {

private static final String ADMIN_CHANNEL_ID = "admin_channel";

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

    //get current user from shared preferences
    SharedPreferences sp = getSharedPreferences("SP_USER", MODE_PRIVATE);
    String savedCurrentUser = sp.getString("Current_USERID", "None");

    /*we have two type notification
                                 > notificationType ="PostNotification"
                                 > notificationType ="ChatNotification"*/

    String notificationType = remoteMessage.getData().get("notificationType");

    if (notificationType.equals("PostNotification")){
        //post notification
        String sender = remoteMessage.getData().get("sent");
        String pId = remoteMessage.getData().get("id");
        String pTitle = remoteMessage.getData().get("name");
        String pDescription = remoteMessage.getData().get("description");

        //if user is same that has post don't show notification
        if (!sender.equals(savedCurrentUser)){
            showPostNotification("" + pId, "" + pTitle, "" + pDescription);
        }

    }
    else if (notificationType.equals("ChatNotification")){
            //chat notification
        String sent = remoteMessage.getData().get("sent");
        String user = remoteMessage.getData().get("user");
        FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
        if (fUser != null && sent.equals(fUser.getUid())) {
            if (!savedCurrentUser.equals(user)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    sendOAndAboveNotification(remoteMessage);
                } else {
                    sendNormalNotification(remoteMessage);
                }
            }
        }
    }



}

private void showPostNotification(String pId, String pTitle, String pDescription) {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationID = new Random().nextInt(3000);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        setupPostNotificationChannel(notificationManager);
    }
    //show post detail activity using post id when notification clicked
    Intent intent = new Intent(this, ClickPostActivity.class);
    intent.putExtra("PostKey", pId);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    //largeIcon
    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.default_avatar);
    //sound for notification
    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "" + ADMIN_CHANNEL_ID)
            .setSmallIcon(R.drawable.default_avatar)
            .setLargeIcon(largeIcon)
            .setContentTitle(pTitle)
            .setContentText(pDescription)
            .setSound(notificationSoundUri)
            .setContentIntent(pendingIntent);
    //show notification
    notificationManager.notify(notificationID, notificationBuilder.build());
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void setupPostNotificationChannel(NotificationManager notificationManager) {
    CharSequence channelName = "New Notification";
    String channelDescription = "Device to device post notification";

    NotificationChannel adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
    adminChannel.setDescription(channelDescription);
    adminChannel.enableLights(true);
    adminChannel.setLightColor(Color.RED);
    adminChannel.enableVibration(true);
    if (notificationManager!=null){
        notificationManager.createNotificationChannel(adminChannel);
    }
}

private void sendNormalNotification(RemoteMessage remoteMessage) {
    String user = remoteMessage.getData().get("user");
    String icon = remoteMessage.getData().get("icon");
    String title = remoteMessage.getData().get("title");
    String body = remoteMessage.getData().get("body");


    RemoteMessage.Notification notification = remoteMessage.getNotification();
    int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
    Intent intent = new Intent(this, ChatActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("visit_user_id", user);
    intent.putExtras(bundle);
    PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(Integer.parseInt(icon))
            .setContentText(body)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defSoundUri)
            .setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int j = 0;
    if (i > 0) {
        j = 1;
    }
    notificationManager.notify(j, builder.build());

}

@TargetApi(Build.VERSION_CODES.O)
private void sendOAndAboveNotification(RemoteMessage remoteMessage) {

    String user = remoteMessage.getData().get("user");
    String icon = remoteMessage.getData().get("icon");
    String title = remoteMessage.getData().get("title");
    String body = remoteMessage.getData().get("body");


    RemoteMessage.Notification notification = remoteMessage.getNotification();
    int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
    Intent intent = new Intent(this, ChatActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("visit_user_id", user);
    intent.putExtras(bundle);
    PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);

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

    OreoAndAboveNotification notification1 = new OreoAndAboveNotification(this);
    Notification.Builder builder = notification1.getONotificatios(title, body, pIntent, defSoundUri, icon);

    int j = 0;
    if (i > 0) {
        j = 1;
    }
    notification1.getManager().notify(j, builder.build());
}

@Override
public void onNewToken(@NonNull String s) {
    super.onNewToken(s);
    //update user yoken
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        //signed in, update token
        updateToken(s);
    }
}

private void updateToken(String tokenRefresh) {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Tokens");
    Token token = new Token(tokenRefresh);
    ref.child(user.getUid()).setValue(token);
}

}

I need help from the guru :) or any help, to solved this problem

  • 1
    Hey, did you ever solve this issue? – Jean Eric Oct 27 '20 at 14:47
  • Just ignore it like mentioned in [this answer](https://stackoverflow.com/questions/64706041/fatal-exception-firebase-messaging-intent-handle-java-lang-noclassdeffounder) by grolschie – Rob Oter Nov 25 '20 at 09:23

0 Answers0