public static ResultPush Ios(IosMessage iosMessage) { ResultPush resultPush = new ResultPush(); X509Certificate2 certificate = new X509Certificate2(File.ReadAllBytes(GetConfig.Certificate.IOS), GetConfig.Certificate.IOSPassword); var appleCert = File.ReadAllBytes(GetConfig.Certificate.IOS); PushBroker push = Pusher(resultPush); push.RegisterAppleService(new ApplePushChannelSettings(false, certificate)); AppleNotificationPayload appleNotificationPayload = new AppleNotificationPayload(iosMessage.Alert, iosMessage.Badge.Value, iosMessage.Sound.ToString()); var appleNotification = new AppleNotification() .ForDeviceToken(iosMessage.DeviceToken) .WithPayload(appleNotificationPayload); if (iosMessage.Sound.HasValue) { appleNotification.WithSound(iosMessage.Sound.ToString()); } if (iosMessage.Badge.HasValue) { appleNotification.WithBadge(iosMessage.Badge.Value); } push.QueueNotification(appleNotification); return(resultPush); }
private static void SendNotification(string deviceToken, string title, string body, int badgeCount, string sound) { //Create our push services broker var push = new PushBroker(); //Wire up the events for all the services that the broker registers push.OnNotificationSent += NotificationSent; push.OnChannelException += ChannelException; push.OnServiceException += ServiceException; push.OnNotificationFailed += NotificationFailed; push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; push.OnChannelCreated += ChannelCreated; push.OnChannelDestroyed += ChannelDestroyed; //------------------------------------------------ //IMPORTANT NOTE about Push Service Registrations //------------------------------------------------ //Some of the methods in this sample such as 'RegisterAppleServices' depend on you referencing the correct //assemblies, and having the correct 'using PushSharp;' in your file since they are extension methods!!! // If you don't want to use the extension method helpers you can register a service like this: //push.RegisterService<WindowsPhoneToastNotification>(new WindowsPhonePushService()); //If you register your services like this, you must register the service for each type of notification //you want it to handle. In the case of WindowsPhone, there are several notification types! //------------------------- // APPLE NOTIFICATIONS //------------------------- //Configure and start Apple APNS // IMPORTANT: Make sure you use the right Push certificate. Apple allows you to generate one for connecting to Sandbox, // and one for connecting to Production. You must use the right one, to match the provisioning profile you build your // app with! var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/OLBPhoneSandboxCertificate.p12")); //IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server // (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false') // If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server // (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true') ApplePushChannelSettings appleSettings = new ApplePushChannelSettings(false, appleCert, "0lbSandb0x"); push.RegisterAppleService(appleSettings); //Extension method AppleNotification notification = new AppleNotification(); notification.ForDeviceToken(deviceToken); AppleNotificationAlert alert = new AppleNotificationAlert(); if (!string.IsNullOrWhiteSpace(title)) { alert.Title = title; } if (!string.IsNullOrWhiteSpace(body)) { alert.Body = body; } notification.WithAlert(alert); if (badgeCount >= 0) { notification.WithBadge(badgeCount); } if (!string.IsNullOrWhiteSpace(sound)) { notification.WithSound(sound); } push.QueueNotification(notification); //--------------------------- // ANDROID GCM NOTIFICATIONS //--------------------------- //Configure and start Android GCM //IMPORTANT: The API KEY comes from your Google APIs Console App, under the API Access section, // by choosing 'Create new Server key...' // You must ensure the 'Google Cloud Messaging for Android' service is enabled in your APIs Console push.RegisterGcmService(new GcmPushChannelSettings("AIzaSyAQTRCFjVX5LQ0dOd4Gue4_mUuv3jlPMrg")); //Fluent construction of an Android GCM Notification //IMPORTANT: For Android you MUST use your own RegistrationId here that gets generated within your Android app itself! push.QueueNotification( new GcmNotification().ForDeviceRegistrationId("APA91bHr5W1cNl5mcZ_iWqGKVnvcXeZwYdVGCCFjt0M8egamRAIq5lCASbUQe-3E9M74CiD8Moildh4SC8Qj6qUUpCnNOQ5v17A9go1enqDipOGSaeiDU_I3fGroneA7tL3FAMlN60nW") .WithJson("{\"alert\":\"Hello Leslie!\",\"badge\":7,\"sound\":\"sound.caf\"}")) ; //----------------------------- // // WINDOWS PHONE NOTIFICATIONS // //----------------------------- // //Configure and start Windows Phone Notifications // push.RegisterWindowsPhoneService(); // //Fluent construction of a Windows Phone Toast notification // //IMPORTANT: For Windows Phone you MUST use your own Endpoint Uri here that gets generated within your Windows Phone app itself! // push.QueueNotification(new WindowsPhoneToastNotification() // .ForEndpointUri(new Uri("DEVICE REGISTRATION CHANNEL URI HERE")) // .ForOSVersion(WindowsPhoneDeviceOSVersion.MangoSevenPointFive) // .WithBatchingInterval(BatchingInterval.Immediate) // .WithNavigatePath("/MainPage.xaml") // .WithText1("PushSharp") // .WithText2("This is a Toast")); // // // //------------------------- // // WINDOWS NOTIFICATIONS // //------------------------- // //Configure and start Windows Notifications // push.RegisterWindowsService(new WindowsPushChannelSettings("WINDOWS APP PACKAGE NAME HERE", // "WINDOWS APP PACKAGE SECURITY IDENTIFIER HERE", "CLIENT SECRET HERE")); // //Fluent construction of a Windows Toast Notification // push.QueueNotification(new WindowsToastNotification() // .AsToastText01("This is a test") // .ForChannelUri("DEVICE CHANNEL URI HERE")); Console.WriteLine("Waiting for Queue to Finish..."); //Stop and wait for the queues to drains push.StopAllServices(); Console.WriteLine("Queue Finished, press return to exit..."); Console.ReadLine(); }