2

내 서버에서 푸시 알림을 최적화하려고합니다. 지금은 그 중 하나를 하나씩 보내고 (오래된 도서관에서), 시간이 좀 걸립니다 (4 시간).하나 이상의 기기 (iOS)에 푸시 알림을 보내는 방법은 무엇입니까?

많은 서비스 토큰을 사용하여 알림을 보내도록 리팩터링했습니다 (지금은 500 개의 토큰을 사용했습니다). 이를 위해 Redth/PushSharp 라이브러리를 사용하고 있습니다. 나는 sample code을 따랐다. 그리고 나는 그것을 severals 장치 토큰에 통지하기 위해 채택했다.

PushService service = new PushService(); 

//Wire up the events 
service.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired); 
service.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged); 
service.Events.OnChannelException += new PushSharp.Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException); 
service.Events.OnNotificationSendFailure += new PushSharp.Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure); 
service.Events.OnNotificationSent += new PushSharp.Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent); 
service.Events.OnChannelCreated += new PushSharp.Common.ChannelEvents.ChannelCreatedDelegate(Events_OnChannelCreated); 
service.Events.OnChannelDestroyed += new PushSharp.Common.ChannelEvents.ChannelDestroyedDelegate(Events_OnChannelDestroyed); 

//Configure and start ApplePushNotificationService 
string p12Filename = ... 
string p12FilePassword = ... 

var appleCert = File.ReadAllBytes(p12Filename); 

service.StartApplePushService(new ApplePushChannelSettings(true, appleCert, p12FilePassword)); 

var appleNotification = NotificationFactory.Apple(); 

foreach (var itemToProcess in itemsToProcess) 
{ 
    itemToProcess.NotificationDateTime = DateTime.Now; 
    mobile.SubmitChanges(); 

    string deviceToken = GetCleanDeviceToken(itemToProcess.MobileDevice.PushNotificationIdentifier); 
    appleNotification.ForDeviceToken(deviceToken); 
} 

service.QueueNotification(appleNotification 
    .WithAlert(itemsToProcess[0].MobileDeviceNotificationText.Text) 
    .WithSound("default") 
    .WithBadge(0) 
    .WithCustomItem("View", itemsToProcess[0].Value.ToString())); 

//Stop and wait for the queues to drains 
service.StopAllServices(true); 

그런 다음 2 개의 기기에 3 개의 알림을 보냈습니다. 첫 번째 장치 만 가져 왔고 (문제는 두 장치를 따로 따로 시도했기 때문에 장치 관련이 아닙니다.) 바로 뒤에 PushChannelBase class에 OperationCanceledException이 발생합니다. 그래서 나는 무엇이 잘못되었는지를 모른다. 어떤 생각?

+0

안녕하세요. 단일 요청으로 단일 장치를 여러 장치에 보낼 수 있습니다. 나는 r & d를 수행하고 코드를 구현하고 5-10 개의 장치로 테스트했습니다. –

답변

3

처리 할 각 항목에 대해 별도의 알림을 대기열에 넣어야합니다.
단일 알림에서 여러 장치 토큰을 설정할 수 없습니다. 수행 중이므로 OperationCanceledException이 발생합니다.

+0

생명의 은인 당신! – Arjan

+0

안녕하세요. 단일 요청으로 단일 장치를 여러 장치에 보낼 수 있습니다. 나는 r & d를 수행하고 코드를 구현하고 5-10 개의 장치로 테스트했습니다. –

1

예 : 콘솔 C# 응용 프로그램

  1. 당신이
  2. 당신이 알림을 당신이 당신의 데이터베이스 내에서 여러 장치 토큰을 저장 한
  3. 유효 생산 및 개발 인증서가있는 것을 전제로

    그 데이터베이스에서 온 것입니다

  4. ,515,도서관

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using PushSharp; 
    using PushSharp.Core; 
    using PushSharp.Apple; 
    using System.IO; 
    
    namespace MyNotification 
    { 
        class Program 
        { 
         //args may take "true" or "false" to indicate the app is running for 
         //development or production (Default = false which means Development) 
         static void Main(string[] args) 
         { 
          bool isProduction = false; 
          if (args != null && args.Length == 1) 
          { 
           Console.Write(args[0] + Environment.NewLine); 
           bool.TryParse(args[0], out isProduction); 
          } 
          try 
          { 
           //Gets a notification that needs sending from database 
           AppNotification notification = AppNotification.GetNotification(); 
           if (notification != null && notification.ID > 0) 
           {   
            //Gets all devices to send the above notification to   
            List<IosDevice> devices = IosDevice.GetDevices(!isProduction); 
            if (devices != null && devices.Count > 0) 
            { 
             PushBroker push = new PushBroker();//a single instance per app 
             //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.OnChannelCreated += ChannelCreated; 
             push.OnChannelDestroyed += ChannelDestroyed; 
             //make sure your certifcates path are all good 
             string apnsCertFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../Certificate/Certificates_Apple_Push_Production.p12"); 
             if (!isProduction) 
              apnsCertFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../Certificate/Certificates_Apple_Push_Development.p12"); 
             var appleCert = File.ReadAllBytes(apnsCertFile); 
             push.RegisterAppleService(new ApplePushChannelSettings(isProduction, appleCert, "135TrID35")); //Extension method 
    
             foreach (IosDevice device in devices) 
             { 
              //if it is required to send additional information as well as the alert message, uncomment objects[] and WithCustomItem 
              //object[] obj = { "North", "5" }; 
    
              push.QueueNotification(new AppleNotification() 
              .ForDeviceToken(device.DeviceToken) 
              .WithAlert(DateTime.Now.ToString())//(notification.AlertMessage) 
               //.WithCustomItem("Link", obj) 
              .WithBadge(device.BadgeCount + 1) 
              .WithSound(notification.SoundFile));//sound.caf 
             } 
             push.StopAllServices(waitForQueuesToFinish: true); 
            } 
           } 
           Console.WriteLine("Queue Finished, press return to exit..."); 
           Console.ReadLine(); 
          } 
          catch (Exception ex) 
          { 
           Console.WriteLine(ex.Message); 
           Console.ReadLine(); 
          } 
         } 
    
         static void NotificationSent(object sender, INotification notification) 
         { 
          Console.WriteLine("Sent: " + sender + " -> " + notification); 
         } 
    
         static void NotificationFailed(object sender, INotification notification, Exception notificationFailureException) 
         { 
          Console.WriteLine("Failure: " + sender + " -> " + notificationFailureException.Message + " -> " + notification); 
         } 
    
         static void ChannelException(object sender, IPushChannel channel, Exception exception) 
         { 
          Console.WriteLine("Channel Exception: " + sender + " -> " + exception); 
         } 
    
         static void ServiceException(object sender, Exception exception) 
         { 
          Console.WriteLine("Service Exception: " + sender + " -> " + exception); 
         } 
    
         static void DeviceSubscriptionExpired(object sender, string expiredDeviceSubscriptionId, DateTime timestamp, INotification notification) 
         { 
          Console.WriteLine("Device Subscription Expired: " + sender + " -> " + expiredDeviceSubscriptionId); 
         } 
    
         static void ChannelDestroyed(object sender) 
         { 
          Console.WriteLine("Channel Destroyed for: " + sender); 
         } 
    
         static void ChannelCreated(object sender, IPushChannel pushChannel) 
         { 
          Console.WriteLine("Channel Created for: " + sender); 
         } 
        } 
    
    } 
    
+0

거기에 각 루프에 대한 푸시 알림을 여러 장치에 보낼 수있는 방법이 있습니까? 벌크 푸시와 같은? 모두 한 번에 .. –

+0

이것은 일괄 푸시를 수행합니다. 각 루프는 푸시 큐에 장치를 추가하기위한 것입니다. 나는 이것을 생산에 사용하고 매력처럼 작동합니다. – user890255

+0

안녕하세요. 단일 요청으로 단일 장치를 여러 장치에 보낼 수 있습니다. 나는 r & d를 수행하고 코드를 구현하고 5-10 개의 장치로 테스트했습니다. –

관련 문제