TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

Register and Unregister for push notifications? #157

Closed Ghevi closed 1 year ago

Ghevi commented 1 year ago

Sorry for opening an issue, but i wonder if there is the possibility to register and unregister manually for push notifications using the cloud messaging package. My app has a login screen, so i would like to start receiving notifications only once the user log in and when he log out, the phone stops receiving the notifications. I see the interface has two methods to subscribe and unsuscribe from topics, but i want to send notifications on single devices not to everyone. I tried this piece of code, but i'm not sure what to do on the Android case:

public void RegisterForNotifications()
    {
#if ANDROID
       // what to do?
#elif IOS
        FirebaseCloudMessagingImplementation.Initialize();
#endif
    }

    public void UnregisterForNotifications()
    {
#if ANDROID
        // Is this correct?
        Firebase.Messaging.FirebaseMessaging.Instance.DeleteToken();
#elif IOS
        UIKit.UIApplication.SharedApplication.UnregisterForRemoteNotifications();
#endif
    }

Thanks!

TobiasBuchholz commented 1 year ago

Hi Ghevi, I think instead of controlling this behavior from the client side you can just solve your requirements from the server side. So when the user logs in, the token should be stored and used by the server to send the notifications to the user and as soon as the user logs out, this token will get removed from the server, so no notifications are delivered any more to this specific user.

Ghevi commented 1 year ago

@TobiasBuchholz i see, as i imagined. If the user needs to be online to login, it also should be able to call the server when logging out and setting the token null. Will try to push for this solution.