开发者

Why iOS Push notification events are not triggered in C#

I'm trying to send iOS push notifications, but the code I'm using is not working due to the events OnNotificationSucceeded and OnNotificationFailed are not triggered.

My code is:

string p12fileName = "path to my iOS push certificate";
string p12password = 开发者_如何学JAVA"my iOS push certificate password";
string deviceToken = "my device ID";

var appleCert = File.ReadAllBytes(p12fileName);
        
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, p12password);
config.ValidateServerCertificate = false;
        
var apnsBroker = new ApnsServiceBroker(config);
        
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;
            Response.Write("Apple Notification Failed: ID={" + apnsNotification.Identifier + "}, Code={" + statusCode + "}");
        }
        else
        {
            // Inner exception might hold more useful information like an ApnsConnectionException
            Response.Write("Notification Failed for some unknown reason : {" + ex.InnerException + "}");
        }

        // Mark it as handled
        return true;
    });
};
apnsBroker.OnNotificationSucceeded += (notification) => {
    LogDao.Info(deviceData.UserId, "Apple Notification Sent!");
};
SureAct.Helpers.LiteralsHelper literals = new SureAct.Helpers.LiteralsHelper(1, 0);

string message = "test";

apnsBroker.Start();
apnsBroker.QueueNotification(new ApnsNotification
{
    DeviceToken = deviceToken,
    Payload = JObject.Parse("{\"aps\":{\"alert\":\"" + message + "\",\"badge\":1,\"sound\":\"default\"}}")
});
apnsBroker.Stop();

I checked the certificate and password and they are correct. And If I try to send a push notification using the webpage pushtry.com I receive the push.

What I'm doing wrong? Why the events are not triggered?

Kind regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜