C# c语言中的Apple推送通知提供程序#
我对苹果推送通知服务一无所知。我正在尝试为我的应用程序实现它。我在谷歌和stackoverflow中都搜索得很好,但并不满意。我正在尝试用c#实现provider。我也试过月饼 这里有谁能给我推荐一个很简单的循序渐进的教程吗。我已经获得了ios开发者和apn证书以及p12密钥。我需要帮助。C# c语言中的Apple推送通知提供程序#,c#,ios,objective-c,push-notification,C#,Ios,Objective C,Push Notification,我对苹果推送通知服务一无所知。我正在尝试为我的应用程序实现它。我在谷歌和stackoverflow中都搜索得很好,但并不满意。我正在尝试用c#实现provider。我也试过月饼 这里有谁能给我推荐一个很简单的循序渐进的教程吗。我已经获得了ios开发者和apn证书以及p12密钥。我需要帮助。 提前感谢。在您使用MoonAPNS时,我建议您阅读图书馆作者的4部分指南。以下是我正在使用的基础结构和流程: 简要概述: 我用于与APNS服务器通信。我有一个SQL Server后端数据库设置来处理发送的所有
提前感谢。在您使用MoonAPNS时,我建议您阅读图书馆作者的4部分指南。以下是我正在使用的基础结构和流程: 简要概述: 我用于与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\U通知
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应用程序通过Web服务调用的:
[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
通过以下方式从多个不同的位置以多个不同的DB调用:
执行[ins\U APNS\U PushNote]
@网络ID
,@iOSApplicationName
,@AlertMessage
,@SoundFile
,@RequestedBy
为虚拟服务器(PushSharp)检索这些APNS请求的SP:
现在来看我对PushSharp应用程序所做的更改。实际上可以归结为两种方法:
静态void Main(字符串[]参数)
{
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...");
}
static void checkForPushRequest()
{
string yourconstring=“yourconnectionstringtothedbgoesher”;
存储过程SP=新的存储过程{
Name=“get\u apnstossend”,
参数=新列表()
};
试一试{
System.Data.DataTable dt=数据库操作。执行_Database_命令(YourConnString,SP,true);
如果((dt!=null)和&!(dt.Rows.Count<1)){
foreach(dt.行中的System.Data.DataRow dRow){
string deviceToken=Convert.ToString(dRow[1]);
string alertMessage=Convert.ToString(dRow[2]);
int badgeNumber=Convert.ToInt16(卓尔[3]);
string soundFile=Convert.ToString(dRow[4]);
字符串apnsCertFileToUse=Convert.ToString(dRow[5]);
sendPush(deviceToken、alertMessage、soundFile、badgeNumber、apnsCertFileToUse);
}
}
}捕获(例外情况除外){
//处理您的异常
}
}
静态void sendPush(string DeviceToken、string AlertMessage、string SoundFile、int-BadgeNumber、string apnsCertFileToUse)
{
//创建我们的服务
PushService push=新的PushService();
//把事件联系起来
push.Events.OnDeviceSubscriptionExpired+=新的PushSharp.Common.ChannelEvents.DeviceSubscriptionExpired(Events\u OnDeviceSubscriptionExpired);
//push.Events.OnDeviceSubscriptionIdChanged+=新的PushSharp.Common.ChannelEvents.DeviceSubscriptionIdChanged(事件\u OnDeviceSubscriptionIdChanged);
push.Events.OnChannelException+=新的PushSharp.Common.ChannelEvents.channelexceptionlegate(Events\OnChannelException);
push.Events.OnNotificationSendFailure+=新的PushSharp.Common.ChannelEvents.NotificationSendFailureDelegate(事件\u OnNotificationSendFailure);
push.Events.OnNotificationSent+=新的PushSharp.Common.ChannelEvents.NotificationSentDelegate(事件\u OnNotificationSent);
//配置并启动Apple APNS
//重要提示:确保使用正确的推送证书。Apple允许您生成一个用于连接到沙盒的推送证书,
//一个用于连接到生产。您必须使用正确的一个,以匹配您构建的资源调配配置文件
//应用程序与!
//这来自ApplicationInfo表。每个支持APNS的应用程序在列中都有自己的certfile名称
字符串certfiletuse=“C:\\APNS\u Certs\\”+apnscertfiletuse;
var appleCert=File.ReadAllBytes(certFileToUse);
//重要提示:如果您使用的是开发配置文件,则必须使用沙盒推送通知服务器
//(因此,您可以将ApplePushChannelSettings的ctor中的第一个参数保留为“false”)
//如果您使用的是临时或AppStore配置文件,则必须使用生产推送通知服务器
//(因此您可以将ApplePushChannelSettings的ctor中的第一个参数更改为“true”)
push.StartApplePushService(新的ApplePushChannelSettings(false,appleCert,“P12PasswordHere”);
//流畅地构建iOS通知
//重要提示:对于iOS,您必须在此处使用自己的DeviceToken,该DeviceToken在应用程序委托时在iOS应用程序本身中生成
//为注册远程通知调用,并将设备令牌传递回给您
push.QueueNotification(NotificationFactory.Apple()
.ForDeviceToken(DeviceToken)
.WithAlert(AlertMessage)
.with声音(声音文件)
.带徽章(徽章编号);
//WriteLine(“等待队列完成…”);
//停下来,等待队列排干
push.StopAllServices(true);
//控制台.WriteLine(“队列完成,按回车退出…”);
}
我在PushSharp解决方案中添加了一个控制台项目,并将控制台部署到APNS服务器。此控制台应用程序根据每分钟运行的计划任务启动
如果你有更多的问题,请告诉我。我在过去的一年里一直在使用这个过程
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...");
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="iosp12.aspx.cs" Inherits="p12ios.iosp12" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">Device token</td>
<td>
<asp:TextBox ID="txtDeviceToken" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">message</td>
<td class="auto-style4">
<asp:TextBox ID="txtMessage" runat="server" Height="44px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" Width="142px" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Web;
using Newtonsoft.Json.Linq;
using PushSharp.Apple;
using System.Collections.Generic;
namespace p12ios
{
public partial class iosp12 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SendPushNotification(txtDeviceToken.Text, txtMessage.Text);
}
private void SendPushNotification(string deviceToken, string message)
{
try
{
//Get Certificate
var appleCert = System.IO.File.ReadAllBytes(Server.MapPath("~/IOS/"p12 certificate""));
// Configuration (NOTE: .pfx can also be used here)
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, appleCert, "p12 Password");
// Create a new broker
var apnsBroker = new ApnsServiceBroker(config);
// Wire up events
apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
{
aggregateEx.Handle(ex =>
{
// See what kind of exception it was to further diagnose
if (ex is ApnsNotificationException)
{
var notificationException = (ApnsNotificationException)ex;
// Deal with the failed notification
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
string desc = $"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}";
Console.WriteLine(desc);
Label1.Text = desc;
}
else
{
string desc = $"Apple Notification Failed for some unknown reason : {ex.InnerException}";
// Inner exception might hold more useful information like an ApnsConnectionException
Console.WriteLine(desc);
Label1.Text = desc;
}
// Mark it as handled
return true;
});
};
apnsBroker.OnNotificationSucceeded += (notification) =>
{
Label1.Text = "Apple Notification Sent successfully!";
};
var fbs = new FeedbackService(config);
fbs.FeedbackReceived += (string devicToken, DateTime timestamp) =>
{
// Remove the deviceToken from your database
// timestamp is the time the token was reported as expired
};
// Start Proccess
apnsBroker.Start();
var payload = new Dictionary<string, object>();
var aps = new Dictionary<string, object>();
aps.Add("alert", "This is a sample notification!");
aps.Add("badge", 1);
aps.Add("sound", "chime.aiff");
payload.Add("aps", aps);
payload.Add("confId", "20");
payload.Add("pageFormat", "Webs");
payload.Add("pageTitle", "Evalu");
payload.Add("webviewURL", "https:/UploadedImages/MobileApp/icons/Datalist-Defg");
payload.Add("notificationBlastID", "");
payload.Add("pushtype", "");
payload.Add("content-available", );
var jsonx = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
if (deviceToken != "")
{
apnsBroker.QueueNotification(new ApnsNotification
{
DeviceToken = deviceToken,
Payload = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(payload))
});
}
apnsBroker.Stop();
}
catch (Exception)
{
throw;
}
}
}
}