使用pushsharp通知

时间:2013-01-26 18:14:53

标签: pushsharp

我想用pushsharp在Web应用程序中实现通知。

我在github上找到了一些他们正在使用的pushspharp的示例项目。

请帮助我开始使用pushsharp开发关于通知的小应用程序。

提前致谢

2 个答案:

答案 0 :(得分:1)

公共类PushNotificationService     {         private PushService _pushService;

    public string MessageDetails { get; set; }
   // public RecipientDeliveryStatus MessageDeliveryStatus { get; set; }

    public PushNotificationService()
    {
        _pushService = new PushService();
        _pushService.Events.OnDeviceSubscriptionExpired += Events_OnDeviceSubscriptionExpired;
        _pushService.Events.OnDeviceSubscriptionIdChanged += Events_OnDeviceSubscriptionIdChanged;
        _pushService.Events.OnChannelException += Events_OnChannelException;
        _pushService.Events.OnNotificationSendFailure += Events_OnNotificationSendFailure;
        _pushService.Events.OnNotificationSent += Events_OnNotificationSent;
        _pushService.Events.OnChannelCreated += Events_OnChannelCreated;
        _pushService.Events.OnChannelDestroyed += Events_OnChannelDestroyed;

    }

    public void SendAppleNotification(string deviceToken, string message)
    {
        var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/PushSharp.Apns.Sandbox.p12"));
        _pushService.StartApplePushService(new ApplePushChannelSettings(appleCert, "pushsharp"));
        _pushService.QueueNotification(NotificationFactory.Apple()
         .ForDeviceToken(deviceToken)
         .WithAlert(message)
         .WithSound("default")
         .WithBadge(7));
        _pushService.StopApplePushService();

    }


    public void SendAndroidNotification(string message, string deviceRegId)
    {
        _pushService.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings("Config.AnrodidSenderId", "Config.AndroidAuthToken", "Config.AndroidApplicationId"));

        _pushService.QueueNotification(NotificationFactory.AndroidGcm()
                                           .ForDeviceRegistrationId(deviceRegId)
                                           .WithCollapseKey("NONE")
                                 .WithJson("{\"alert\":\"" + message + "\",\"badge\":\"7\"}"));
        _pushService.StopGoogleCloudMessagingPushService();
    }


    public void SendWindowsNotification()
    {
        _pushService.StartWindowsPhonePushService(new WindowsPhonePushChannelSettings());
        //Configure and start Windows Notifications
        _pushService.StartWindowsPushService(new WindowsPushChannelSettings("677AltusApps.PushSharpTest",
            "ms-app://s-1-15-2-397915024-884168245-3562497613-3307968140-4074292843-797285123-433377759", "ei5Lott1HEbbZBv2wGDTUsrCjU++Pj8Z"));

        //Fluent construction of a Windows Toast Notification
        _pushService.QueueNotification(NotificationFactory.Windows().Toast().AsToastText01("This is a test").ForChannelUri("YOUR_CHANNEL_URI_HERE"));
        _pushService.StopWindowsPhonePushService();
    }
    void Events_OnDeviceSubscriptionIdChanged(PlatformType platform, string oldDeviceInfo, string newDeviceInfo, Notification notification)
    {

      //  MessageDeliveryStatus = RecipientDeliveryStatus.Undeliverable;
        MessageDetails = "Device subscription id has changed";
    }

    void Events_OnNotificationSent(Notification notification)
    {

        //MessageDeliveryStatus = RecipientDeliveryStatus.Sent;
        MessageDetails = "Message Sent";
    }

    void Events_OnNotificationSendFailure(Notification notification, Exception notificationFailureException)
    {

        //MessageDeliveryStatus = RecipientDeliveryStatus.Undeliverable;
        MessageDetails = notificationFailureException.Message;
    }

    void Events_OnChannelException(Exception exception, PlatformType platformType, Notification notification)
    {

       // MessageDeliveryStatus = RecipientDeliveryStatus.Undeliverable;
        MessageDetails = exception.Message;
    }

    void Events_OnDeviceSubscriptionExpired(PlatformType platform, string deviceInfo, Notification notification)
    {

       // MessageDeliveryStatus = RecipientDeliveryStatus.Undeliverable;
        MessageDetails = "Device Subscription Expired";
    }

    void Events_OnChannelDestroyed(PlatformType platformType, int newChannelCount)
    {
       // MessageDeliveryStatus = RecipientDeliveryStatus.Undeliverable;
        MessageDetails = "Channel Descrtroyed";
    }

    void Events_OnChannelCreated(PlatformType platformType, int newChannelCount)
    {
        //MessageDeliveryStatus = RecipientDeliveryStatus.Undeliverable;
        MessageDetails = "Channel Created";
    }
}

答案 1 :(得分:1)

我有一个.net 3.5应用程序,我需要使用PushSharp,所以我最终在pushSharp周围创建了一个WebApi包装器,这样就可以像简单RESTful一样使用它API。

我已将代码放在GitHub上以防任何人需要它。 PushSharp.Web

https://github.com/has-taiar/PushSharp/tree/master/PushSharp.Web