我们正在开发适用于 iOS 设备的 MDM 解决方案。
我们已经能够通过网站安装带有 MDM 有效负载的配置文件,并且我们已经收到 iOS 设备发送的包含 PushMagic、deviceToken 和其他值的 PUT 请求。
我们使用以下描述创建了 COMPANY.pem SSL 证书: http://www.softhinker.com/in-the-news/iosmdmvendorcsrsigning
我们尝试使用库push-sharp发送推送通知: https://github.com/Redth/PushSharp
我们使用 MDM.p12 文件构建和这些命令
openssl pkcs12 -export -in ./COMPANY.pem -inkey ./customerPrivateKey.pem -certfile ./CertificateSigningRequest.certSigningRequest -out MDM.p12
发送此通知的程序不会抛出任何错误并正常退出。 但我们的设备上没有收到任何推送通知,也没有通过反馈服务收到任何信息。
还要提到的是,我们也尝试使用沙箱服务器和生产服务器。
这是我们使用 Push-sharp 库的代码:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using PushSharp;
using PushSharp.Apple;
namespace PushSharp.Sample
{
class Program
{
static void Main(string[] args)
{
//Create our service
PushService push = new PushService();
//Wire up the events
push.Events.OnDeviceSubscriptionExpired += new Common.ChannelEvents.DeviceSubscriptionExpired(Events_OnDeviceSubscriptionExpired);
push.Events.OnDeviceSubscriptionIdChanged += new Common.ChannelEvents.DeviceSubscriptionIdChanged(Events_OnDeviceSubscriptionIdChanged);
push.Events.OnChannelException += new Common.ChannelEvents.ChannelExceptionDelegate(Events_OnChannelException);
push.Events.OnNotificationSendFailure += new Common.ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
push.Events.OnNotificationSent += new 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!
//var appleCert = File.ReadAllBytes("C:\\TEMP\\apns-mdm.p12");
var appleCert = File.ReadAllBytes(@".\MDM.p12");
//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(true, appleCert, "PWD"));
//String p12File = @".\apns-mdm.p12";
//String p12Password = "PWD";
String pushMagicString = "00454668-00B2-4122-A1DC-72ACD64E6AFB";
//String deviceToken = "27asObngxvVNb3RvRMs3XVaEWC1DNa3TjFE12stKsig=";
//Configure and start Android GCM
//IMPORTANT: The SENDER_ID is your Google API Console App Project ID.
// Be sure to get the right Project ID from your Google APIs Console. It's not the named project ID that appears in the Overview,
// but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
// where 785671162406 is the project id, which is the SENDER_ID to use!
//push.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings("785671162406", "AIzaSyC2PZNXQDVaUpZGmtsF_Vp8tHtIABVjazI", "com.pushsharp.test"));
//Configure and start Windows Phone Notifications
//push.StartWindowsPhonePushService(new WindowsPhone.WindowsPhonePushChannelSettings());
//Fluent construction of a Windows Phone Toast notification
//push.QueueNotification(NotificationFactory.WindowsPhone().Toast()
//.ForEndpointUri(new Uri("http://sn1.notify.live.net/throttledthirdparty/01.00/AAFCoNoCXidwRpn5NOxvwSxPAgAAAAADAgAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQ"))
//.ForOSVersion(WindowsPhone.WindowsPhoneDeviceOSVersion.MangoSevenPointFive)
//.WithBatchingInterval(WindowsPhone.BatchingInterval.Immediate)
//.WithNavigatePath("/MainPage.xaml")
//.WithText1("PushSharp")
//.WithText2("This is a Toast"));
//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
String test = "3d 58 64 4d 90 d3 18 09 22 5c 50 d2 12 16 b5 67 71 1e be 5c 13 6e 41 3c 3e 81 b5 52 30 68 09 a5";
test = test.Replace(" ", string.Empty);
Console.WriteLine("Device Token length is: " + test.Length);
Console.WriteLine("DeviceToken is: " + test);
Console.WriteLine("PushMagic is: " + pushMagicString);
DateTime dayAfterTomorrow = DateTime.Now.AddDays(2);
Console.WriteLine("Expiry date is: " + dayAfterTomorrow.ToString());
push.QueueNotification(NotificationFactory.Apple()
.ForDeviceToken(test).WithExpiry(dayAfterTomorrow).WithCustomItem("mdm", pushMagicString));
//push.Events.RaiseNotificationSent(NotificationFactory.Apple()
// .ForDeviceToken(hex).WithCustomItem("mdm", pushMagicString));
//Fluent construction of an Android GCM Notification
//push.QueueNotification(NotificationFactory.AndroidGcm()
// .ForDeviceRegistrationId("APA91bG7J-cZjkURrqi58cEd5ain6hzi4i06T0zg9eM2kQAprV-fslFiq60hnBUVlnJPlPV-4K7X39aHIe55of8fJugEuYMyAZSUbmDyima5ZTC7hn4euQ0Yflj2wMeTxnyMOZPuwTLuYNiJ6EREeI9qJuJZH9Zu9g")
// .WithCollapseKey("NONE")
// .WithJson("{\"alert\":\"Alert Text!\",\"badge\":\"7\"}"));
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...");
Console.ReadLine();
}
static void Events_OnDeviceSubscriptionIdChanged(Common.PlatformType platform, string oldDeviceInfo, string newDeviceInfo)
{
//Currently this event will only ever happen for Android GCM
Console.WriteLine("Device Registration Changed: Old-> " + oldDeviceInfo + " New-> " + newDeviceInfo);
}
static void Events_OnNotificationSent(Common.Notification notification)
{
Console.WriteLine("Sent: " + notification.Platform.ToString() + " -> " + notification.ToString());
}
static void Events_OnNotificationSendFailure(Common.Notification notification, Exception notificationFailureException)
{
Console.WriteLine("Failure: " + notification.Platform.ToString() + " -> " + notificationFailureException.Message + " -> " + notification.ToString());
}
static void Events_OnChannelException(Exception exception)
{
Console.WriteLine("Channel Exception: " + exception.ToString());
}
static void Events_OnDeviceSubscriptionExpired(Common.PlatformType platform, string deviceInfo)
{
Console.WriteLine("Device Subscription Expired: " + platform.ToString() + " -> " + deviceInfo);
}
}
}
我们使用此网站对收到的 deviceToken 进行编码: http://home.paulschou.net/tools/xlate/ 从 base64 到 HEX。
我们还在 iOS 设备上使用 APS/PC 日志记录配置文件,以通过 IPCU 提供的调试控制台获取更多输出。
最佳答案
发送到推送通知服务的消息不得包含 aps
键。消息应仅包含 PushMagic
字符串作为 mdm
键的值。例如,使用您定义为 pushMagicString
的值,发送到推送通知服务的消息应如下所示:
{"mdm":"00454668-00B2-4122-A1DC-72ACD64E6AFB"}
我没有使用过你正在使用的push-sharp库。但是,我简要浏览了代码,似乎 AppleNotificationPayload.ToJson 方法总是向消息添加 aps 键。我建议注释掉第 114 行,然后尝试一下。这是 ToJson
方法,其中注释掉了该行:
public string ToJson()
{
JObject json = new JObject();
JObject aps = new JObject();
if (!this.Alert.IsEmpty)
{
if (!string.IsNullOrEmpty(this.Alert.Body)
&& string.IsNullOrEmpty(this.Alert.LocalizedKey)
&& string.IsNullOrEmpty(this.Alert.ActionLocalizedKey)
&& (this.Alert.LocalizedArgs == null || this.Alert.LocalizedArgs.Count <= 0)
&& !this.HideActionButton)
{
aps["alert"] = new JValue(this.Alert.Body);
}
else
{
JObject jsonAlert = new JObject();
if (!string.IsNullOrEmpty(this.Alert.LocalizedKey))
jsonAlert["loc-key"] = new JValue(this.Alert.LocalizedKey);
if (this.Alert.LocalizedArgs != null && this.Alert.LocalizedArgs.Count > 0)
jsonAlert["loc-args"] = new JArray(this.Alert.LocalizedArgs.ToArray());
if (!string.IsNullOrEmpty(this.Alert.Body))
jsonAlert["body"] = new JValue(this.Alert.Body);
if (this.HideActionButton)
jsonAlert["action-loc-key"] = new JValue((string)null);
else if (!string.IsNullOrEmpty(this.Alert.ActionLocalizedKey))
jsonAlert["action-loc-key"] = new JValue(this.Alert.ActionLocalizedKey);
aps["alert"] = jsonAlert;
}
}
if (this.Badge.HasValue)
aps["badge"] = new JValue(this.Badge.Value);
if (!string.IsNullOrEmpty(this.Sound))
aps["sound"] = new JValue(this.Sound);
if (this.ContentAvailable.HasValue)
aps["content-available"] = new JValue(this.ContentAvailable.Value);
//json["aps"] = aps;
foreach (string key in this.CustomItems.Keys)
{
if (this.CustomItems[key].Length == 1)
json[key] = new JValue(this.CustomItems[key][0]);
else if (this.CustomItems[key].Length > 1)
json[key] = new JArray(this.CustomItems[key]);
}
string rawString = json.ToString(Newtonsoft.Json.Formatting.None, null);
StringBuilder encodedString = new StringBuilder();
foreach (char c in rawString)
{
if ((int)c < 32 || (int)c > 127)
encodedString.Append("\\u" + String.Format("{0:x4}", Convert.ToUInt32(c)));
else
encodedString.Append(c);
}
return rawString;// encodedString.ToString();
}
关于Apple 的 C# MDM 服务器,无法发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11542629/