我正在尝试在开发模式下在天蓝色推送通知集线器中注册带有标签的设备令牌,但是在天蓝色门户网站中它显示0个已注册的活动设备,当我检查通知中心时,它将显示一个注册正在进行。
以下是我的示例代码:
App代表:
var deviceTokenDes = deviceToken.Description;
if (!string.IsNullOrWhiteSpace(deviceTokenDes))
{
deviceTokenDes = deviceTokenDes.Trim('<');
deviceTokenDes = deviceTokenDes.Trim('>');
deviceTokenDes = deviceTokenDes.Replace(" ", "");
DeviceToken = deviceTokenDes.Trim('<');
DeviceToken = deviceTokenDes.Trim('>');
DeviceToken = deviceTokenDes.Replace(" ", "");
}
Hub = new SBNotificationHub(myapp.ListenConnectionString, myapp.NotificationHubName);
登录视图模型:
var tags = new List<string> { userId };
AppDelegate.Hub?.UnregisterAllAsync(AppDelegate.DeviceToken, error =>
{
if (error != null)
{
Console.WriteLine("Error calling Unregister: {0}", error);
}
AppDelegate.Hub.RegisterNativeAsync(AppDelegate.DeviceToken, new NSSet(tags.ToArray()), errorCallback =>
{
if (errorCallback != null)
{
Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
}
});
答案 0 :(得分:0)
这就是我们在AppDelegate类中所做的一切。
public override async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
bool ShouldComplete = true;
// Validate if we have already got a registration
try
{
string validation = NSUserDefaults.StandardUserDefaults.StringForKey("InitialTagRegistration");
if (validation.Contains("Completed"))
{
ShouldComplete = false;
}
}
catch (Exception genEx)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx);
}
Hub = new SBNotificationHub(ConfigurableSettings.NotificationHubConnectionString, ConfigurableSettings.NotificationHubPathName);
ApplicationState.SetValue("NotificationHub", Hub);
// Get previous device token
NSData oldDeviceToken = await ApplicationSettings.RetrieveDeviceToken();
// If the token has changed unregister the old token and save the new token to UserDefaults.
if (oldDeviceToken != null)
{
if (oldDeviceToken.ToString() != deviceToken.ToString())
{
try
{
Hub.UnregisterAllAsync(oldDeviceToken, (error) =>
{
//check for errors in unregistration process.
if (error != null)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + error + " | Source: " + "Unregistering old device token against the notification hub.");
//exit out of the code here because we can't keep our hub clean without being able to remove the device from our registration list.
return;
}
else
{
ShouldComplete = true;
}
});
}
catch (Exception genEx)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx + Environment.NewLine + Environment.NewLine);
}
}
}
else
{
// Store current device token
bool res = await ApplicationSettings.CacheDeviceToken(deviceToken);
}
// Check if we need to perform our initial registrations
if (ShouldComplete)
{
NSSet RegisteredTags = await ApplicationSettings.RetrieveUserTags();
if (RegisteredTags == null)
{
RegisteredTags = new NSSet("AppleDevice");
}
//Register the device against the notification hub keeping the details accurate at all times.
Hub.RegisterNativeAsync(deviceToken, RegisteredTags, (errorCallback) =>
{
if (errorCallback != null)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + errorCallback + " | Source: " + "Registering device token against the notification hub.");
}
else
{
if (deviceToken != null)
{
NSUserDefaults.StandardUserDefaults.SetString("Completed", "InitialTagRegistration");
NSUserDefaults.StandardUserDefaults.Synchronize();
}
}
});
}
}
它的长短之处在于,在将设备令牌传递到azure通知中心之前,您无需对设备令牌执行任何操作。这就是为我们解决问题的原因,我们已经在一个活跃的应用程序中运行了几个月而没有任何问题。希望这会有所帮助。
编辑:为了在用户登录时更新标签,我们存储设备令牌以供以后使用,当用户登录时,我们在单独的类中使用以下方法以便于更新标签:
public static async Task<bool> UpdateTags(StaffProfile user)
{
//Get the instance of the Notification hub
SBNotificationHub UpdateHub = new SBNotificationHub(ConfigurableSettings.NotificationHubConnectionString, ConfigurableSettings.NotificationHubPathName);
//Grab the current device token that was stored during the start up process.
NSData CurrentDeviceToken = await ApplicationSettings.RetrieveDeviceToken();
//Get and create the tags we want to use.
string EmailTag = string.Empty;
string StoreTag = string.Empty;
string OrganisationTag = "AppleDevice:OrgTag";
string GenericTag = "AppleDevice:StaffTag";
if (!string.IsNullOrWhiteSpace(user.Email))
{
EmailTag = user.Email;
//Remove unwanted spaces and symbols.
EmailTag = EmailTag.Replace(" ", "");
EmailTag = string.Format("AppleDevice:{0}", EmailTag);
}
if (!string.IsNullOrWhiteSpace(user.Store?.Name))
{
StoreTag = user.Store.Name;
//Remove unwanted space.
StoreTag = StoreTag.Replace(" ", "");
StoreTag = string.Format("AppleDevice:{0}", StoreTag);
}
//Create array of strings to the currently fixed size of 3 items.
NSString[] TagArray = new NSString[4];
//Only add in the tags that contain data.
if (!string.IsNullOrEmpty(EmailTag)) { TagArray[0] = (NSString)EmailTag; }
if (!string.IsNullOrEmpty(StoreTag)) { TagArray[1] = (NSString)StoreTag; }
if (!string.IsNullOrEmpty(OrganisationTag)) { TagArray[2] = (NSString)OrganisationTag; }
if (!string.IsNullOrEmpty(GenericTag)) { TagArray[3] = (NSString)GenericTag; }
NSSet tags = new NSSet(TagArray);
// Store our tags into settings
ApplicationSettings.CacheUserTags(tags);
try
{
if (CurrentDeviceToken == null)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: Device token is empty." + Environment.NewLine + Environment.NewLine);
}
else
{
UpdateHub.RegisterNativeAsync(CurrentDeviceToken, tags, (error) =>
{
//check for errors in unregistration process.
if (error != null)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + error + " | Source: " + "Registering against hub with new tags." + Environment.NewLine + Environment.NewLine);
// Lets do this so that we can force the initial registration to take place again.
NSUserDefaults.StandardUserDefaults.SetString("Failed", "InitialTagRegistration");
NSUserDefaults.StandardUserDefaults.Synchronize();
}
else
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[INFORMATION] - Message: Successful Registration - Source: Registering against hub with new tags." + Environment.NewLine + Environment.NewLine);
}
});
}
}
catch (Exception genEx)
{
ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx + Environment.NewLine + Environment.NewLine);
}
return await Task.FromResult(true);
}
答案 1 :(得分:0)
我尝试过停止代码,但我没有收到任何通知。第一次当我在azure通知中心发送测试通知时,它会显示一次成功,但没有收到通知,在第二次之后它显示0通过,0表示失败。
根据您的描述,我建议您可以直接与APN通信,以检查是否可以获得错误响应以缩小此问题。以下是一些有用的教程,您可以参考它们并按如下方式解决问题:
官方文档:$unwind
Communicating with APNs,Apple推送通知服务(APN)的调试应用程序
Knuff,一个服务器端库,用于向iOS / OSX(APNS),Android / Chrome(GCM),Windows / Windows Phone,Amazon(ADM)和Blackberry设备发送通知。有关如何配置和发送Apple推送通知,您可以参考PushSharp。