【问题标题】:Pushsharp 4.0 with Firebase带有 Firebase 的 Pushsharp 4.0
【发布时间】:2017-03-20 03:15:57
【问题描述】:

阅读大量帖子后,我找不到一个完整的示例,如何使用带有 Firebase 的 Pushsharp 4.0 发送 GCM 推送通知。 PushSharp 的许多示例都使用旧的 Google 云消息传递,而不是 Firebase 和/或旧的 PushSharp 版本。

有没有人提供使用带有 Firebase 的 PushSharp 4.0 发送 GCM 推送通知的稳定且有效的代码示例?

【问题讨论】:

  • 你找到答案了吗?我也会感兴趣的。谢谢

标签: firebase firebase-cloud-messaging pushsharp


【解决方案1】:

你可以试试这个:https://github.com/Redth/PushSharp/issues/711

我自己没有尝试过,但从上面帖子中的 cmets 看来,人们在 PushSharp 和 Firebase 上取得了成功,并提出了建议的更改。

【讨论】:

  • 感谢 yuo 的回复,但是我在网上搜索时看到了 GitHub 上的评论,但我一直在寻找整个代码,特别是 GcmConfiguration 设置和 QueueNotification,我发现了不同的实现在网上,我不知道哪个是正确的
【解决方案2】:

我能够相对轻松地让 PushSharp 与 FCM 一起工作。我以为会难很多。以下是我采取的步骤:

  1. 我在 console.firebase.google.com 上创建了一个新项目
  2. 然后我转到设置 => 云消息传递。我将该 SenderID 用作我的 PushSharp 发件人 ID,并将旧版服务器密钥用作我的 PushSharp 身份验证密钥。
  3. 接下来,我进入 google play 控制台,选择项目,然后选择 Development Tools => Services and API。
  4. 然后我通过在“Linked Sender ID”字段中输入 SenderID 将 App 项目链接到新的 FCM 项目。

这应该是您为使服务器正常工作所做的一切。

现在,我需要将应用程序连接到这个新的 SenderID。我认为这通常是通过输入 google-services.json 文件来完成的,如 Firebase 设置中所示。但是,我使用 PhoneGap/Cordova 插件来发送通知。所以,我这样做:

  1. 将 GCM_SENDER_ID 更改为上面的 SenderID。

不幸的是,我不得不重新分发新版本的应用程序才能使其正常工作。但它确实奏效了。

希望这对其他人有所帮助。

【讨论】:

  • 在第 3 步中,您是指 Google Cloud Console 吗?如果是这种情况,在控制台上激活 FCM 后,我只会看到我们为所有 Google Cloud Api 服务看到的带有凭据、配额和指标选项卡的常规仪表板。我找不到“链接的发件人 ID”。你能帮我找到它吗?谢谢。
  • 好吧,看看它,我在 Google Play 控制台中看不到 SenderID。基于此:stackoverflow.com/questions/39392627/…,看起来您只需将发件人 ID 放入应用程序的 google-services.json 文件中。我可以发誓它曾经在那里。
  • 啊,我明白了。感谢检查出来。我会试试的。
【解决方案3】:

这个控制台程序对我来说很好用。

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
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 2016-08-22
    • 1970-01-01
    • 2012-02-11
    • 2010-12-18
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多