在阅读了很多帖子后,我找不到一个完整的例子,如何在Firebase中使用Pushsharp 4.0发送GCM推送通知。很多使用PushSharp的例子都是使用旧的谷歌云消息,而不是Firebase和/或旧的PushSharp版本。
有没有人有一个使用PushSharp 4.0和Firebase发送GCM推送通知的稳定和有效的代码示例?
发布于 2016-11-07 16:52:29
你可以试试这个:https://github.com/Redth/PushSharp/issues/711
我自己还没有尝试过,但从上面帖子的评论来看,似乎人们已经成功地使用了PushSharp和Firebase,并提出了修改建议。
发布于 2018-04-19 10:05:50
我能够让PushSharp相对容易地与FCM一起工作。我以为这会难得多。以下是我采取的步骤:
我在console.firebase.google.com
这应该是服务器正常工作所要做的全部工作。
现在,我需要将应用程序连接到这个新的SenderID。我认为这通常是通过输入google-services.json文件来完成的,如Firebase设置中所示。但是,我使用PhoneGap/Cordova插件来发送通知。所以,我这样做:
不幸的是,我不得不重新发布新版本的应用程序才能让它正常工作。但它确实起作用了。
希望这对其他人有帮助。
发布于 2018-11-08 16:30:50
这个控制台程序对我来说运行得很好。
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
}
}https://stackoverflow.com/questions/40453990
复制相似问题