- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我对 Apple 推送通知服务完全陌生。我正在尝试为我的应用程序实现它。我在 google 和 stackoverflow 中搜索得很好,但并不满意。我正在尝试在 C# 中实现提供程序。我也尝试过 MoonAPNs。
这里的任何人都可以向我推荐非常简单的分步教程。我已经获得了 ios 开发者和 apn 证书以及 p12 key 。那我需要帮助。提前致谢。
最佳答案
这是我正在使用的基础架构和流程:
简要概述:我用 PushSharp用于与 APNS 服务器通信。我有一个 SQL Server 后端数据库设置来处理所有发送的订阅和通知。我还有一个虚拟服务器(实际上是几个),所有服务器都复制了 .p12 证书。这些服务器具有检查表中是否有任何需要发出的推送通知的进程,然后将数据集传递给 PushSharp 进程。
详细规范:表 1 - APNS_订阅
CREATE TABLE [dbo].[APNS_Subscriptions](
[id] [int] IDENTITY(1,1) NOT NULL,
[DeviceToken] [varchar](250) NULL,
[DeviceID] [varchar](250) NULL,
[NetworkID] [varchar](250) NULL,
[Application] [varchar](250) NULL,
[AddedOn] [datetime] NULL,
[Active] [bit] NULL,
[Dev] [bit] NULL,
[BadgeCount] [int] NOT NULL,
CONSTRAINT [PK_APNSSubscriptions] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
表 2 - APNS_PushNotifications
CREATE TABLE [dbo].[APNS_PushNotifications](
[id] [int] IDENTITY(1,1) NOT NULL,
[DeviceToken] [varchar](250) NULL,
[AlertMessage] [varchar](250) NULL,
[BadgeNumber] [int] NULL,
[SoundFile] [varchar](250) NULL,
[ApplicationName] [varchar](250) NULL,
[AddedOn] [datetime] NULL,
[AddedBy] [varchar](250) NULL,
[ProcessedOn] [datetime] NULL,
[ViewedOnDeviceDateTime] [datetime] NULL,
CONSTRAINT [PK_APNS_PushNotifications] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我通过这个 SP 添加订阅(这是通过每个实现 APNS 的 iPhone 应用程序通过网络服务调用的:
[ins_APNS_Sub]
@MyDeviceID VARCHAR(250) ,
@MyDeviceToken VARCHAR(250) ,
@MyApplicationName VARCHAR(250)
AS
DECLARE @Count AS INT
SET @Count = ( SELECT COUNT(id)
FROM dbo.APNS_Subscriptions
WHERE DeviceID = @MyDeviceID
AND DeviceToken = @MyDeviceToken
AND [Application] = @MyApplicationName
)
IF @Count = 0
BEGIN
DECLARE @NetworkID AS VARCHAR(250)
SET @NetworkID = ( SELECT TOP 1
networkid
FROM dbo.AuthenticatedDevices
WHERE deviceid = @MyDeviceID
AND COALESCE(banned, 0) = 0
ORDER BY lastupdatedon DESC
)
IF @NetworkID IS NOT NULL
BEGIN
INSERT INTO dbo.APNS_Subscriptions
( DeviceToken ,
DeviceID ,
NetworkID ,
[Application] ,
AddedOn ,
Active
)
VALUES ( @MyDeviceToken , -- DeviceToken - varchar(250)
@MyDeviceID , -- DeviceID - varchar(250)
@NetworkID , -- NetworkID - varchar(250)
@MyApplicationName , -- Application - varchar(250)
CURRENT_TIMESTAMP , -- AddedOn - datetime
1 -- Active - bit
)
END
END
通过此 SP 添加推送通知:
[ins_APNS_PushNote]
@MyNetworkID VARCHAR(250) , -- NetworkID of recipient or ALL to go to all recipients
@MyApplicationName VARCHAR(250) , -- Application Name for the iOS app
@APNSAlertMessage VARCHAR(225) , -- Alert Message (Required)
@APNSSoundFile VARCHAR(250) = NULL ,
@WhoRequested VARCHAR(250) -- Process Name that called this SP
AS
-- Get the current badge count, make a temp table and increment the appropriate rows in the Sub table
DECLARE @UpdateTable AS TABLE
(
DeviceToken VARCHAR(250) ,
NetworkID VARCHAR(250) ,
ApplicationName VARCHAR(250) ,
BadgeCount INT
)
IF @MyNetworkID = 'ALL'
BEGIN
INSERT INTO @UpdateTable
( DeviceToken ,
NetworkID ,
ApplicationName ,
BadgeCount
)
SELECT DeviceToken ,
NetworkID ,
[Application] ,
BadgeCount
FROM dbo.APNS_Subscriptions
WHERE [Application] = @MyApplicationName
AND COALESCE(Dev, 0) = 0
UPDATE @UpdateTable
SET BadgeCount = BadgeCount + 1
UPDATE sub
SET sub.BadgeCount = temp.BadgeCount
FROM dbo.APNS_Subscriptions sub
INNER JOIN @UpdateTable temp ON temp.DeviceToken = sub.DeviceToken
AND temp.NetworkID = sub.NetworkID
AND temp.ApplicationName = sub.[Application]
INSERT INTO dbo.APNS_PushNotifications
( DeviceToken ,
AlertMessage ,
BadgeNumber ,
SoundFile ,
ApplicationName ,
AddedOn ,
AddedBy
)
SELECT sub.DeviceToken ,
@APNSAlertMessage ,
temp.BadgeCount ,
@APNSSoundFile ,
@MyApplicationName ,
CURRENT_TIMESTAMP ,
@WhoRequested
FROM dbo.APNS_Subscriptions sub
INNER JOIN dbo.AuthenticatedDevices ad ON ad.deviceid = sub.DeviceID
INNER JOIN @UpdateTable temp ON temp.DeviceToken = sub.DeviceToken
AND temp.ApplicationName = sub.[Application]
WHERE COALESCE(ad.banned, 0) = 0
AND sub.[Application] = @MyApplicationName
-- AND ad.networkid = @MyNetworkID
AND COALESCE(sub.Dev, 0) = 0
END
ELSE
BEGIN
DECLARE @Count AS INT = ( SELECT COUNT(id)
FROM dbo.APNS_Subscriptions
WHERE NetworkID = @MyNetworkID
AND Active = 1
AND [Application] = @MyApplicationName
)
IF @Count = 0
BEGIN
RETURN
END
INSERT INTO @UpdateTable
( DeviceToken ,
NetworkID ,
ApplicationName ,
BadgeCount
)
SELECT DeviceToken ,
NetworkID ,
[Application] ,
BadgeCount
FROM dbo.APNS_Subscriptions
WHERE [Application] = @MyApplicationName
AND COALESCE(Dev, 0) = 0
AND NetworkID = @MyNetworkID
UPDATE @UpdateTable
SET BadgeCount = BadgeCount + 1
UPDATE sub
SET sub.BadgeCount = temp.BadgeCount
FROM dbo.APNS_Subscriptions sub
INNER JOIN @UpdateTable temp ON temp.DeviceToken = sub.DeviceToken
AND temp.NetworkID = sub.NetworkID
AND temp.ApplicationName = sub.[Application]
INSERT INTO dbo.APNS_PushNotifications
( DeviceToken ,
AlertMessage ,
BadgeNumber ,
SoundFile ,
ApplicationName ,
AddedOn ,
AddedBy
)
SELECT sub.DeviceToken ,
@APNSAlertMessage ,
temp.BadgeCount ,
@APNSSoundFile ,
@MyApplicationName ,
CURRENT_TIMESTAMP ,
@WhoRequested
FROM dbo.APNS_Subscriptions sub
INNER JOIN dbo.AuthenticatedDevices ad ON ad.deviceid = sub.DeviceID
INNER JOIN @UpdateTable temp ON temp.DeviceToken = sub.DeviceToken
AND temp.ApplicationName = sub.[Application]
WHERE COALESCE(ad.banned, 0) = 0
AND sub.[Application] = @MyApplicationName
AND sub.networkid = @MyNetworkID
AND COALESCE(sub.Dev, 0) = 0
AND COALESCE(sub.Active, 0) = 1
END
这是从几个不同数据库中的几个不同地方调用的:执行 [ins_APNS_PushNote] @网络ID ,@iOSApplicationName ,@AlertMessage ,@声音文件 ,@RequestedBy
为虚拟服务器 (PushSharp) 检索这些 APNS 请求的 SP:
[get_APNSToSend]
AS
BEGIN
DECLARE @CurrentTimestamp AS DATETIME = CURRENT_TIMESTAMP
UPDATE dbo.APNS_PushNotifications
SET ProcessedOn = CURRENT_TIMESTAMP
WHERE ProcessedOn IS NULL
SELECT id ,
DeviceToken ,
AlertMessage ,
BadgeNumber ,
SoundFile ,
ai.APNSDistCertFile AS APNSCertFile
FROM dbo.APNS_PushNotifications apns
INNER JOIN dbo.ApplicationInfo ai ON ai.ApplicationName = apns.ApplicationName
WHERE ProcessedOn = @CurrentTimestamp
AND ai.APNSDistCertFile IS NOT NULL
END
现在介绍我对 PushSharp 应用所做的更改。真的只是归结为两种方法:静态无效主要(字符串[]参数) { checkForPushRequest();
static void checkForPushRequest()
{
string YourConnString = "YourConnectionStringToTheDBGoesHere";
Stored_Procedure SP = new Stored_Procedure {
Name = "get_APNSToSend",
Parameters = new List<SqlParameter>()
};
try {
System.Data.DataTable dt = DatabaseOperations.Execute_Database_Command(YourConnString, SP, true);
if ((dt != null) && !(dt.Rows.Count < 1)) {
foreach (System.Data.DataRow dRow in dt.Rows) {
string deviceToken = Convert.ToString(dRow[1]);
string alertMessage = Convert.ToString(dRow[2]);
int badgeNumber = Convert.ToInt16(dRow[3]);
string soundFile = Convert.ToString(dRow[4]);
string apnsCertFileToUse = Convert.ToString(dRow[5]);
sendPush(deviceToken, alertMessage, soundFile, badgeNumber, apnsCertFileToUse);
}
}
} catch (Exception ex) {
// Handle your exception
}
}
static void sendPush(string DeviceToken, string AlertMessage, string SoundFile, int BadgeNumber, string apnsCertFileToUse)
{
//Create our service
PushService push = new PushService();
//Wire up the events
push.Events.OnDeviceSubscriptionExpired += new PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
//push.Events.OnDeviceSubscriptionIdChanged += new PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
push.Events.OnChannelException += new PushSharp.Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
push.Events.OnNotificationSendFailure += new PushSharp.Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
push.Events.OnNotificationSent += new PushSharp.Common.ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);
//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!
// This comes from the ApplicationInfo table. Each app that supports APNS has it's own certfile name in the column
string certFileToUse = "C:\\APNS_Certs\\" + apnsCertFileToUse;
var appleCert = File.ReadAllBytes(certFileToUse);
//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')
push.StartApplePushService(new ApplePushChannelSettings(false, appleCert, "P12PasswordHere"));
//Fluent construction of an iOS notification
//IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
// for registered for remote notifications is called, and the device token is passed back to you
push.QueueNotification(NotificationFactory.Apple()
.ForDeviceToken(DeviceToken)
.WithAlert(AlertMessage)
.WithSound(SoundFile)
.WithBadge(BadgeNumber));
//Console.WriteLine("Waiting for Queue to Finish...");
//Stop and wait for the queues to drains
push.StopAllServices(true);
// Console.WriteLine("Queue Finished, press return to exit...");
}
我在 PushSharp 解决方案中添加了一个控制台项目,并将控制台部署到 APNS 服务器。此控制台应用程序根据每分钟运行的计划任务触发。
如果您有更多问题,请告诉我。去年我一直在企业环境中使用这个过程,没有遇到任何问题。完美运行。
关于c# - Apple 在 C# 中推送通知提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277018/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!