0

I am trying to create a personal chat application for my college project. I have created the chat activity where the user can chat and receive the message, now I want to add the notification feature to it so that user can be notified at every message received by user, for this, I have searched all over the internet and found the FCM (firebase cloud messing) and tried to use it it didn't work. below is the code that I have used to show a notification to the user.

  public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getNotification().getBody());

    Log.d(TAG, "From: " + remoteMessage.getFrom());


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

        if (true) {

            scheduleJob();
        } else {

            handleNow();
        }

    }


    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }


}

@Override
public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);


    sendRegistrationToServer(token);
}

private void scheduleJob() {

    OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(MyWorker.class)
            .build();
    WorkManager.getInstance().beginWith(work).enqueue();

}

private void handleNow() {
    Log.d(TAG, "Short lived task is done.");
}


private void sendRegistrationToServer(String token) {
    // TODO: Implement this method to send token to your app server.
}


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

    String channelId = "chat";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle("Title")
                    .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());
}
 }

maybe I have used it in a wrong way ....help me so that I can complete my project

NANB
  • 35
  • 4
  • You cannot send a message through FCM from one device directly to another device, as that would allow all users of your app to send whatever message they want to all other users of your app. Sending messages to a device requires that you run code on a trusted environment, such as your development machine, a server you control, or Cloud Functions. See https://stackoverflow.com/questions/37990140/how-to-send-one-to-one-message-using-firebase-messaging/37993724#37993724 – Frank van Puffelen Mar 06 '20 at 14:25
  • After reading that blog I have found that I have to use the node.js code and deploy to the firebase function and then call the sendNotificationToUser() method whenever a new message is been sent. but didn't get the proper idea where to call the FirebaseMessaging.getInstance().subscribeToTopic(). in the code.....have i got it right or not – NANB Mar 06 '20 at 14:48
  • 1) Node.js is just what I used in the blog. Nowadays most folks use Cloud Functions, which is a way to run small node scripts on Google's servers. 2) You can call `subscribeToTopic()` pretty much anywhere in your code. If that is what your question was about, I highly recommend including that in your post. As it stands, while I understand that FCM isn't working as you'd expect/like it to work, I'm having a hard time understand what you're asking. – Frank van Puffelen Mar 06 '20 at 15:03
  • I'm developing a messaging app just using firebase. After sending a message, I also send a notification request to firebase server. You can do the same way if you know FCM token of receiver – Kasım Özdemir Mar 06 '20 at 16:59

0 Answers0