I have successfully developed a chat app using java and firebase and I am stuck at sending and receiving chat notifications when chatting with a friend who has install my app. i have tried using FCM but to no avail. I have written the code and when i send a message, the friend doesn't receive a notification whether app is in foreground or background as well as no errors are found. please help me out. Below is the code for that class that extends the FirebaseMessagingService
.
@SuppressLint("MissingFirebaseInstanceTokenRefresh")
public class NotifyFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull @NotNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
SharedPreferences preferences = getSharedPreferences("PREFS", MODE_PRIVATE);
String currentUser = preferences.getString("currentuser", "none");
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if(firebaseUser != null && sent.equals(firebaseUser.getUid())) {
if (!currentUser.equals(user)) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
sendOreoNotification(remoteMessage);
}
else{
sendNotification(remoteMessage);
}
}
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void sendOreoNotification(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();
assert user != null;
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoNotification oreoNotification = new OreoNotification(this);
Notification.Builder builder = oreoNotification.getOreoNotification(title,body,pendingIntent,defaultSound,icon);
int i = 0;
if(j>0){
i = j;
}
oreoNotification.getManager().notify(i, builder.build());
}
private void sendNotification(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();
assert user != null;
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int i = 0;
if(j>0){
i = j;
}
notificationManager.notify(i, builder.build());
}
}