在生产环境中使用带有以下推送通知证书的企业帐户时,我的应用程序收到推送通知:
苹果生产iOS推送服务
然后,为了在App Store中发布我的应用程序,我开始使用App Store帐户,无论我如何尝试,Apple都会使用以下名称创建生产证书:
苹果推送服务
然后从SO得知苹果已经更改了证书的命名。我的问题是,我在服务器端使用Push Sharp并出现以下错误:
您选择了生产服务器,但是您的证书没有
似乎是生产证书!请检查以确保您
有正确的证书!
给出的两种解决方案都不起作用。
我将Push Sharp从2.2更新到3.0 beta,遇到了很多编译错误,甚至PushSharp类本身都不存在,然后从该线程尝试了另一个解决方案。
从this下载了PushSharp,并通过删除生产线对其进行了重新编译,但仍然出现相同的错误。另外,该解决方案还不是很清楚,我不确定是否要对证书检查行发表评论,或者是什么,我尝试了所有但没有运气
如何解决这个问题?
最佳答案
您可以开始使用新的PushSharp 3.0.1稳定版,它确实非常简单而且很棒。
首先,您应该转到并删除所有与pushsharp 2相关的dll:
等也请确保删除:
然后转到您的项目并打开nuget管理器,然后搜索PushSharp 3的最新稳定版本并下载它。
现在,您应该使用一个新的Pushsharp 3 API,该API几乎没有什么不同,但是更加简单:
首先创建一个包含所需的所有代理的类:
public class AppPushBrokers
{
public ApnsServiceBroker Apns { get; set; }
public GcmServiceBroker Gcm { get; set; }
public WnsServiceBroker wsb { get; set; }
}
然后在您的业务逻辑/控制器/ ViewModel等上,您将需要编写您的PushNotification业务:
public class NewPushHandler
{
#region Constants
public const string ANDROID_SENDER_AUTH_TOKEN = "xxxx";
public const string WINDOWS_PACKAGE_NAME = "yyyy";
public const string WINDOWS_PACKAGE_SECURITY_IDENTIFIER = "zzzz";
public const string WINDOWS_CLIENT_SECRET = "hhhh";
public const string APPLE_APP_NAME = "yourappname";
public const string APPLE_PUSH_CERT_PASS = "applecertpass";
#endregion
#region Private members
bool useProductionCertificate;
string appleCertificateType;
String appleCertName;
String appleCertPath;
byte[] appCertData;
// logger
ILogger logger;
// Config (1- Define Config for each platform)
ApnsConfiguration apnsConfig;
GcmConfiguration gcmConfig;
WnsConfiguration wnsConfig;
#endregion
#region Constructor
public NewPushHandler()
{
// Initialize
useProductionCertificate = true; // you can set it dynamically from config
appleCertificateType = useProductionCertificate == true ? "production.p12" : "development.p12";
appleCertName = APPLE_APP_NAME + "-" + appleCertificateType;
appleCertPath = Path.Combine(Application.StartupPath, "Crt", appleCertName); // for web you should use HttpContext.Current.Server.MapPath(
appCertData = File.ReadAllBytes(appleCertPath);
var appleServerEnv = ApnsConfiguration.ApnsServerEnvironment.Production;
logger = LoggerHandler.CreateInstance();
// 2- Initialize Config
apnsConfig = new ApnsConfiguration(appleServerEnv, appCertData, APPLE_PUSH_CERT_PASS);
gcmConfig = new GcmConfiguration(ANDROID_SENDER_AUTH_TOKEN);
wnsConfig = new WnsConfiguration(WINDOWS_PACKAGE_NAME, WINDOWS_PACKAGE_SECURITY_IDENTIFIER, WINDOWS_CLIENT_SECRET);
}
#endregion
#region Private Methods
#endregion
#region Public Methods
public void SendNotificationToAll(string msg)
{
// 3- Create a broker dictionary
var apps = new Dictionary<string, AppPushBrokers> { {"com.app.yourapp",
new AppPushBrokers {
Apns = new ApnsServiceBroker (apnsConfig),
Gcm = new GcmServiceBroker (gcmConfig),
wsb = new WnsServiceBroker(wnsConfig)
}}};
#region Wire Up Events
// 4- events to fires onNotification sent or failure for each platform
#region Android
apps["com.app.yourapp"].Gcm.OnNotificationFailed += (notification, aggregateEx) =>
{
aggregateEx.Handle(ex =>
{
// See what kind of exception it was to further diagnose
if (ex is GcmConnectionException)
{
// Something failed while connecting (maybe bad cert?)
Console.WriteLine("Notification Failed (Bad APNS Connection)!");
}
else
{
Console.WriteLine("Notification Failed (Unknown Reason)!");
}
// Mark it as handled
return true;
});
};
apps["com.app.yourapp"].Gcm.OnNotificationSucceeded += (notification) =>
{
//log success here or do what ever you want
};
#endregion
#region Apple
apps["com.app.yourapp"].Apns.OnNotificationFailed += (notification, aggregateEx) =>
{
aggregateEx.Handle(ex =>
{
// See what kind of exception it was to further diagnose
if (ex is ApnsNotificationException)
{
var apnsEx = ex as ApnsNotificationException;
// Deal with the failed notification
var n = apnsEx.Notification;
logger.Error("Notification Failed: ID={n.Identifier}, Code={apnsEx.ErrorStatusCode}");
}
else if (ex is ApnsConnectionException)
{
// Something failed while connecting (maybe bad cert?)
logger.Error("Notification Failed (Bad APNS Connection)!");
}
else
{
logger.Error("Notification Failed (Unknown Reason)!");
}
// Mark it as handled
return true;
});
};
apps["com.app.yourapp"].Apns.OnNotificationSucceeded += (notification) =>
{
Console.WriteLine("Notification Sent!");
};
#endregion
#endregion
#region Prepare Notification
// 5- prepare the json msg for android and ios and any platform you want
string notificationMsg = msg;
string jsonMessage = @"{""message"":""" + notificationMsg +
@""",""msgcnt"":1,""sound"":""custom.mp3""}";
string appleJsonFormat = "{\"aps\": {\"alert\":" + '"' + notificationMsg + '"' + ",\"sound\": \"default\"}}";
#endregion
#region Start Send Notifications
// 6- start sending
apps["com.app.yourapp"].Apns.Start();
apps["com.app.yourapp"].Gcm.Start();
//apps["com.app.yourapp"].wsb.Start();
#endregion
#region Queue a notification to send
// 7- Queue messages
apps["com.app.yourapp"].Gcm.QueueNotification(new GcmNotification
{
// You can get this from database in real life scenarios
RegistrationIds = new List<string> {
"ppppp",
"nnnnn"
},
Data = JObject.Parse(jsonMessage),
Notification = JObject.Parse(jsonMessage)
});
apps["com.app.yourapp"].Apns.QueueNotification(new ApnsNotification
{
DeviceToken = "iiiiiii",
Payload = JObject.Parse(appleJsonFormat)
});
#endregion
#region Stop Sending Notifications
//8- Stop the broker, wait for it to finish
// This isn't done after every message, but after you're
// done with the broker
apps["com.app.yourapp"].Apns.Stop();
apps["com.app.yourapp"].Gcm.Stop();
//apps["com.app.yourapp"].wsb.Stop();
#endregion
}
#endregion
}
重要笔记:
有时,特别是当您使用IIS时,您会发现与IOS证书相关的例外,也是最常见的例外:
“无法识别提供给软件包的凭据”
这是由于多种原因造成的,例如您的应用程序池用户特权或用户帐户(而非本地计算机)上安装的证书,因此尝试从iis禁用模拟身份验证还请检查它是否真的有用Apple PushNotification and IIS
关于ios - 推送通知中的生产证书错误-PushSharp,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35408704/