4

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 ?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Stefano
  • 200
  • 4
  • 15

3 Answers3

3

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.

user2107373
  • 427
  • 3
  • 16
  • Thank yuo for the response, however I saw that comment on GitHub while searching on the web, but I was looking for the whole code, specially for the GcmConfiguration settings and for QueueNotification where I have found different implementation on the web and I don't know which is the correct one – Stefano Nov 07 '16 at 10:50
2

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.

Dicer
  • 396
  • 6
  • 13
  • on step 3, did you mean Google Cloud Console? If that is the case, after activating FCM on the console I only see that regular dashboard that we see for all Google Cloud Api Services with the credentials, quota and metrics tab. I can't find the "Linked Sender Id". Would you be able to help me find it? Thanks. – Rodrigo Lira May 14 '19 at 19:20
  • Well, looking at it, I don't see the SenderID in the Google Play Console anymore. Based on this: https://stackoverflow.com/questions/39392627/problems-with-google-developer-console-sender-id-for-gcm, it looks like you just take the sender id and put it in the app's google-services.json file. I could swear it used to be in there. – Dicer May 15 '19 at 18:29
  • ah, i see. thanks for checking it out. I'll try that. – Rodrigo Lira May 15 '19 at 18:47
1

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
    }
}
u boot
  • 11
  • 1