首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Firebase的Pushsharp 4.0

带有Firebase的Pushsharp 4.0
EN

Stack Overflow用户
提问于 2016-11-07 04:10:16
回答 3查看 7.1K关注 0票数 4

在阅读了很多帖子后,我找不到一个完整的例子,如何在Firebase中使用Pushsharp 4.0发送GCM推送通知。很多使用PushSharp的例子都是使用旧的谷歌云消息,而不是Firebase和/或旧的PushSharp版本。

有没有人有一个使用PushSharp 4.0和Firebase发送GCM推送通知的稳定和有效的代码示例?

EN

回答 3

Stack Overflow用户

发布于 2016-11-07 16:52:29

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

我自己还没有尝试过,但从上面帖子的评论来看,似乎人们已经成功地使用了PushSharp和Firebase,并提出了修改建议。

票数 3
EN

Stack Overflow用户

发布于 2018-04-19 10:05:50

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

我在console.firebase.google.com

  • Then上创建了一个新项目,我转到了settings =>
  1. Messaging。我使用该SenderID作为我的PushSharp发件人ID,旧版服务器密钥作为我的PushSharp身份验证密钥。
  2. 接下来,我转到google play控制台,选择项目,然后选择开发工具=>服务和应用程序接口。
  3. 然后我通过在“链接的发件人ID”字段中输入SenderID将应用程序项目链接到新的=>项目。

这应该是服务器正常工作所要做的全部工作。

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

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

不幸的是,我不得不重新发布新版本的应用程序才能让它正常工作。但它确实起作用了。

希望这对其他人有帮助。

票数 2
EN

Stack Overflow用户

发布于 2018-11-08 16:30:50

这个控制台程序对我来说运行得很好。

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40453990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档