Push notification 如何向多个设备(iOS)发送推送通知?
我正在尝试优化服务器上的推送通知。现在,我一个接一个地(用一个旧图书馆)发送这些文件,需要一段时间(4个小时) 我重构了我的服务,用很多设备令牌发送通知(目前我尝试用500个令牌发送通知)。为此,我正在使用图书馆。我遵循了下面的步骤,然后对其进行了调整,将通知发送到几个设备令牌Push notification 如何向多个设备(iOS)发送推送通知?,push-notification,apple-push-notifications,pushsharp,Push Notification,Apple Push Notifications,Pushsharp,我正在尝试优化服务器上的推送通知。现在,我一个接一个地(用一个旧图书馆)发送这些文件,需要一段时间(4个小时) 我重构了我的服务,用很多设备令牌发送通知(目前我尝试用500个令牌发送通知)。为此,我正在使用图书馆。我遵循了下面的步骤,然后对其进行了调整,将通知发送到几个设备令牌 PushService service = new PushService(); //Wire up the events service.Events.OnDeviceSubscriptionExpired += n
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个通知。只有第一个设备得到了它们(问题与设备无关,因为我分别使用了这两个设备)。
紧接着,将在中抛出OperationCanceledException。所以我不知道怎么了。有什么想法吗?您应该为每个要处理的项目单独排队通知。
不可能在一个通知上设置多个设备令牌。将发生OperationCanceledException,因为您需要。您应该为要处理的每个项目排队单独的通知。
不可能在一个通知上设置多个设备令牌。将发生OperationCanceledException,因为您需要执行此操作。示例:控制台C#应用程序 这假设
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用PushSharp;
使用PushSharp.Core;
使用PushSharp.Apple;
使用System.IO;
名称空间MyNotification
{
班级计划
{
//args可以采用“true”或“false”来表示应用程序正在运行
//开发或生产(默认值=false,表示开发)
静态void Main(字符串[]参数)
{
bool isProduction=false;
if(args!=null&&args.Length==1)
{
Console.Write(args[0]+Environment.NewLine);
bool.TryParse(args[0],不生产);
}
尝试
{
//获取需要从数据库发送的通知
AppNotification notification=AppNotification.GetNotification();
if(notification!=null&¬ification.ID>0)
{
//获取要向其发送上述通知的所有设备
List devices=IosDevice.GetDevices(!isProduction);
如果(设备!=null&&devices.Count>0)
{
PushBroker push=new PushBroker();//每个应用程序一个实例
//连接代理注册的所有服务的事件
push.OnNotificationSent+=NotificationSent;
push.OnChannelException+=ChannelException;
push.OnServiceException+=ServiceException;
push.OnNotificationFailed+=NotificationFailed;
push.OnDeviceSubscriptionExpired+=设备SubscriptionExpired;
push.OnChannelCreated+=ChannelCreated;
push.onchanneldestrocted+=channeldestrocted;
//确保你的认证路径都是好的
字符串apnsCertFile=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“../../Certificate/Certificates\u Apple\u Push\u Production.p12”);
如果(!i生产)
apnsCertFile=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“../../Certificate/Certificates\u Apple\u Push\u Development.p12”);
var appleCert=File.ReadAllBytes(apnsCertFile);
RegisterAppleService(新的ApplePushChannelSettings(iProduction,appleCert,“135TrID35”);//扩展方法
foreach(设备中的设备)
{
//如果需要发送其他信息以及警报消息,请取消对对象[]和WithCustomItem的注释
//对象[]obj={“北”,“5”};
push.QueueNotification(新应用程序化()
.ForDeviceToken(设备.DeviceToken)
.WithAlert(DateTime.Now.ToString())/(notification.AlertMessage)
//.WithCustomItem(“链接”,obj)
.带徽章(设备.徽章计数+1)
.WithSound(notification.SoundFile));//sound.caf
}
push.StopAllServices(waitForQueuesToFinish:true);
}
}
控制台.WriteLine(“队列完成,按回车退出…”);
Console.ReadLine();
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
Console.ReadLine();
}
}
静态无效通知已发送(对象发送方、INotification通知)
{
Console.WriteLine(“已发送:“+发送方+”->“+通知”);
}
静态无效通知失败(对象发送方、INotification通知、异常通知失败异常)
{
Console.WriteLine(“失败:+发送方+”->“+通知失败异常.消息+”->“+通知”);
}
静态void ChannelException(对象发送器、IPushChannel通道、异常)
{
Console.WriteLine(“通道异常:+sender+”->“+Exception”);
}
静态void ServiceException(对象发送方,异常)
{
Console.WriteLine(“服务异常:+sender+”->“+Exception”);
}
静态无效设备SubscriptionExpired(对象发送方,st
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);
}
}
}