Reputation: 1839
I created an app that has chat in it and when users send messages it shows a notification to the receiver.
Now, I had like that when the user gets a notification and clicks on it, it will open the activity with all of its functionality.
The notification that I make looks like this:
public void notifyThis(String title, String message, String uniqueID, String var1, String var2, String var3) {
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("ChatID",var1);
intent.putExtra("ReceiverID",var2);
intent.putExtra("SenderID",var3);
int RandomID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder groupBuilder = new NotificationCompat.Builder(this,"191919")
.setSmallIcon(R.drawable.ic_launcher_custom_background)
.setGroup(uniqueID)
.setGroupSummary(true);
NotificationCompat.Builder messageBuilder = new NotificationCompat.Builder(this, "191919")
.setSmallIcon(R.drawable.ic_launcher_custom_background)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroup( uniqueID )
.setAutoCancel( true );
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(uniqueID,0, groupBuilder.build());
notificationManager.notify(RandomID, messageBuilder.build());
}
So far everything works ok and when I debug I see that var1, var2, var3
are not null and exist.
My problem is that when I click on the notification, the app crashes since it says:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
But I can't understand why it gets SenderID to be null if the intent of the notification is sent with value in it and if I read it like this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_chat );
getSupportActionBar().hide();
auth = FirebaseAuth.getInstance();
chatID = getIntent().getExtras().getString( "ChatID" );
receiverID = getIntent().getExtras().getString( "ReceiverID" );
SenderID = getIntent().getExtras().getString("SenderID");
if (SenderID.equals( auth.getUid() )) { HERE I GET THE ERROR SAYING SenderID IS NULL
NotMyID = receiverID;
} else {
NotMyID = SenderID;
}
}
Am I reading something wrong? From my understanding I send the SenderID in my notification intent and then I getExtras
from that intent and it should work.
Thank you
Upvotes: 1
Views: 621
Reputation: 627
Just do some changes to the notifyThis function:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Upvotes: 1