对于推送通知,我正在使用C#服务。
在服务器端发送通知但推送通知未到达设备。
我在下面添加了我的服务器端代码和ios代码,请检查并帮助我。
提前谢谢。
Ios代码:
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
-(void)registerRemoteNotifications:(UIApplication *)application {
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
}
(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[[NSUserDefaults standardUserDefaults] setObject:devToken forKey:kRIDeviceToken];
[[NSUserDefaults standardUserDefaults] synchronize];
}
(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:kRIDeviceToken];
[[NSUserDefaults standardUserDefaults] synchronize];
}
服务器端代码(c#):
int port = 2195;
String hostname = "gateway.push.apple.com";
String certificatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/ios/pushNotificationCert.p12");
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "xxxxxxx");
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0);
writer.Write((byte)0);
writer.Write((byte)32);
writer.Write(HexStringToByteArray(notificationId.ToUpper()));
String payload = "{\"aps\":{\"alert\":\"" + "Hi,This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"sound\":\"default\"}}";
writer.Write((byte)0);
writer.Write((byte)payload.Length);
byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
sslStream.Write(array);
sslStream.Flush();
client.Close();
}
catch (System.Security.Authentication.AuthenticationException ex)
{
client.Close();
}
答案 0 :(得分:0)
您将推送发送到Apple生产服务器。有两个服务器,生产和开发(也称为沙箱)。 但是,您几乎肯定在开发模式下运行应用程序,并且开发应用程序和生产服务器不兼容。更改为使用沙盒服务器。