Stefano
Stefano

Reputation: 210

Pushsharp 4.0 with Firebase

After reading lot of post I could not find a complete example how to send GCM push notifications using Pushsharp 4.0 with Firebase. Lot of examples with PushSharp are using the old Google cloud messaging, not Firebase and/or the old PushSharp version.

Does anyone have an example of a stable and working code for sending GCM push notifications using PushSharp 4.0 with Firebase ?

Upvotes: 4

Views: 7444

Answers (3)

u boot
u boot

Reputation: 11

This console program worked fine for me.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            string token = "putYourSecretTokenHere";
            using (var s = new FcmPushNotificationService())
            {
                s.SendPushNotification(token);
                Console.ReadLine();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadLine();
        }
    }
    public sealed class FcmPushNotificationService : IDisposable
    {
        #region Constructors

        public FcmPushNotificationService()
        {
            string serverKey = "Legacy server key";
            this.Config = new GcmConfiguration(serverKey);
            this.Config.GcmUrl = "https://fcm.googleapis.com/fcm/send";

            this.Broker = this.InitializeBroker();
        }

        #endregion


        #region Properties

        private GcmServiceBroker Broker { get; }

        private GcmConfiguration Config { get; }

        #endregion


        #region Private Methods

        private GcmServiceBroker InitializeBroker()
        {
            var gcmServiceBroker = new GcmServiceBroker(this.Config);
            gcmServiceBroker.OnNotificationSucceeded += this.OnNotificationSucceeded;
            gcmServiceBroker.OnNotificationFailed += this.OnNotificationFailed;

            gcmServiceBroker.Start();

            return gcmServiceBroker;
        }

        #endregion


        #region Event Handlers

        private void OnNotificationFailed(GcmNotification gcmNotification, AggregateException aggregateEx)
        {
            aggregateEx.Handle(ex =>
            {
                // See what kind of exception it was to further diagnose
                if (ex is GcmNotificationException notificationException)
                {
                    Console.WriteLine($"Notification of {string.Join(", ", notificationException.Notification.RegistrationIds)} failed: {notificationException.Message}");
                }
                else if (ex is GcmMulticastResultException multicastException)
                {
                    Console.WriteLine($"Notification of {string.Join(", ", multicastException.Succeeded.SelectMany(n => n.RegistrationIds))} succeeded.");
                    Console.WriteLine($"Notification of {string.Join(", ", multicastException.Failed.SelectMany(n => n.Key.RegistrationIds))} failed: {multicastException.Message}");
                }
                else if (ex is DeviceSubscriptionExpiredException expiredException)
                {
                    Console.WriteLine($"Device registration id expired: {expiredException.OldSubscriptionId}. Device registration id changed to {expiredException.NewSubscriptionId}");
                }
                else if (ex is RetryAfterException retryException)
                {
                    Console.WriteLine($"FCM rate limited, don't send more until after {retryException.RetryAfterUtc}");
                }
                else
                {
                    Console.WriteLine($"Failed to send notification {ex}");
                }

                // Mark it as handled
                return true;
            });
        }

        private void OnNotificationSucceeded(GcmNotification gcmNotification)
        {
            Console.WriteLine($"Notification sent to {string.Join(", ", gcmNotification.RegistrationIds)}. Data: {gcmNotification.Data}, Notification: {gcmNotification.Notification}");
        }

        #endregion


        #region IDisposable Members

        /// <inheritdoc cref="IDisposable"/>
        public void Dispose()
        {
            this.Broker?.Stop();
        }

        #endregion


        #region IPushNotificationService Members

        ///<inheritdoc/>
        public void SendPushNotification(string token)
        {
            var notification = JObject.Parse("{\"title\": \"Test\",\"body\": \"Success!\"}");

            this.Broker.QueueNotification(new GcmNotification
            {
                RegistrationIds = new List<string> { token },
                Notification = notification
            });
        }

        #endregion
    }
}

Upvotes: 1

Dicer
Dicer

Reputation: 426

I was able to get PushSharp to work with FCM relatively easily. I thought it would be much harder. Here are the steps I took:

  1. I created a new project on console.firebase.google.com
  2. Then I went to settings => Cloud Messaging. I used that SenderID as my PushSharp sender ID and Legacy Server Key as my PushSharp Authentication Key.
  3. Next, I went to the google play console, selected the project, then Development Tools => Services and API.
  4. Then I linked the App project to the new FCM project by entering the SenderID in the "Linked Sender ID" field.

That should be all you do for the server to be working.

Now, I needed to hook up the App to this new SenderID. I think this is usually done by entering the google-services.json file as shown in the Firebase setup. However, I use PhoneGap/Cordova plug in for sending notifications. So, I do this:

  1. Change the GCM_SENDER_ID to the SenderID above.

Unfortunately, I had to redistribute the new version of the app to get it to work. But it did work.

Hope this helps someone else.

Upvotes: 2

user2107373
user2107373

Reputation: 437

You can try this: https://github.com/Redth/PushSharp/issues/711

I haven't tried for myself but from the comments in above post, it appears people had success with PushSharp and Firebase with the suggested change.

Upvotes: 3

Related Questions