0

I'm just using firebase, now I've managed to create user authentication and have a few push notification questions in the firebase.

Before I followed the tutorial here, and it worked I was very happy. But apparently after I logout and try push notification, I still still receive the notification.

So my question is also pretty much about push notifications, namely:

  1. How to make the notification only enter when I'm logged in?
  2. How do I target the notification with the registered UID only?
  3. Is it necessary to make notifications from firebase, so it can't be as straightforward as an automatic chat application?

Here's the code I've made:

MyFirebaseInstanceldService.java

public class MyFirebaseInstanceldService extends FirebaseInstanceIdService {


    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        // 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.
        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String refreshedToken) {

    }
}

MyFirebaseMessaginService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public MyFirebaseMessagingService() {
    }

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

        sendNotification(Objects.requireNonNull(remoteMessage.getNotification()).getBody());
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, HotelManagementActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

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

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setSmallIcon(R.drawable.rotarylogo);
        notificationBuilder.setContentTitle("Rotary Barcode");
        notificationBuilder.setContentText(messageBody);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setContentIntent(pendingIntent);


        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        assert notificationManager != null;
        notificationManager.notify(0, notificationBuilder.build());
    }
}

If there is anything I still need to add, just say for the other code. I thank you very much for wanting to help me who is still a beginner.

AL.
  • 36,815
  • 10
  • 142
  • 281
Uray Febri
  • 373
  • 4
  • 16
  • 1
    you can add condition inside `onMessageReceived()` to check user is logged in or not and on that condition generate notification, like if(condition...userLoggedin{sendNotification....}) – Nirav Bhavsar Aug 23 '18 at 06:52
  • i has trying this, but it's same auth = FirebaseAuth.getInstance(); if (auth.getCurrentUser() != null) { sendNotification(Objects.requireNonNull(remoteMessage.getNotification()).getBody()); } – Uray Febri Aug 23 '18 at 07:05
  • are you using `mAuth.signOut();` while logout ?? – Nirav Bhavsar Aug 23 '18 at 07:08
  • absolutely yes. – Uray Febri Aug 23 '18 at 07:09
  • you are saying that after you `signout`, still you are getting auth.getCurrentUser() != null as true ??? – Nirav Bhavsar Aug 23 '18 at 07:23
  • right, when I logout I still receive the notification which should not. What should I fix? – Uray Febri Aug 23 '18 at 07:57
  • you can try one more solution, it is like remove your `device token` in firebase databse in which you have saved, before you signout. – Nirav Bhavsar Aug 23 '18 at 08:02
  • I'm sorry, what does mean device token? where i should do that? – Uray Febri Aug 23 '18 at 08:06
  • it is individual token that identifies the unique device to which you are sending notification. – Nirav Bhavsar Aug 23 '18 at 08:09
  • @UrayFebri If you intend to switch to [Cloud Firestore](https://firebase.google.com/docs/firestore/), note that I have exaplained in one of my **[tutorials](https://www.youtube.com/playlist?list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee)** step by step, how you can send **[notifications](https://www.youtube.com/watch?v=6RzB4HXzQyA&t=3s&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=17)** to specific users using `Cloud Firestore` and `Node.js`. – Alex Mamo Aug 23 '18 at 11:45

1 Answers1

1
  1. How to make the notification only enter when I'm logged in?

You have to handle it in a way where the token that gets generated is paired to the user that successfully logs in, making sure that you handle the logout as well.

  1. How do I target the notification with the registered UID only?

Depending on which server you are using, you could just pair the token to the UID of the user (I presume you are using Firebase Auth). For example, what we did in our app (we're using Firebase DB) was as soon as the user gets to the home screen (successfully signs in), we submit the token to the DB, where the key is the uid. This way, we can easily get the token by specifying the uid.

  1. Is it necessary to make notifications from firebase, so it can't be as straightforward as an automatic chat application?

I'm not sure what you mean by this one.


In general, having 3 questions in a single post is not advised here in Stack Overflow. I would strongly suggest that moving forward that you split each question into their own post as much as possible.

AL.
  • 36,815
  • 10
  • 142
  • 281