下面是我们用来注册的代码。
iOS器件
NSString *mobileServicesURL = @"Endpoint=sb://mobilepushnotificationhub.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=XXXXXXXXXXXXXXXXX=";
SBNotificationHub *hub = [[SBNotificationHub alloc] initWithConnectionString:mobileServicesURL notificationHubPath:@"notificationhubname"];
[hub registerNativeWithDeviceToken:token tags:[NSSet setWithObjects:[NSString stringWithFormat:@"iphoneapp_%@", [self getUserID]], nil] completion:^(NSError* error) {
completion(error);
}];Android设备
private void gcmPush() {
NotificationsManager.handleNotifications(this, SENDER_ID, MyHandler.class);
gcm = GoogleCloudMessaging.getInstance(this);
String connectionString = "Endpoint=sb://mobilepushnotificationhub.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXX=";
hub = new NotificationHub("notificationhubname", connectionString, this);
registerWithNotificationHubs();
// completed Code
}
// Added Method
@SuppressWarnings("unchecked")
private void registerWithNotificationHubs() {
new AsyncTask() {
@Override
protected Object doInBackground(Object... params) {
try {
String regid = gcm.register(SENDER_ID);
Log.e("regid RECEIVED ", regid);
hub.register(regid, "androidapp_" + WhatsOnIndiaConstant.USERId);
WhatsOnIndiaConstant.notificationHub = hub;
WhatsOnIndiaConstant.gcmHub = gcm;
} catch (Exception ee) {
Log.e("Exception ", ee.getMessage().toString());
return ee;
}
return null;
}
}.execute(null, null, null);
}发布于 2015-03-13 10:29:18
每次用户卸载和重新安装应用程序时,都会返回一个新的令牌。因此,对于给定的标签,多个注册导致重复推送到同一设备。
据我所知,Apple Push Notification Service一次只有一个可用的设备令牌(也请参阅这里),因此在iOS下一个设备的多个有效设备令牌不会出现问题,但是您可以为一个设备令牌进行多个Azure Notification Hub注册。为了避免这种情况,您必须检查是否已经有具体设备令牌的注册,如果是的话,重用并清理它们:
ASP.NET WebAPI-后端示例
// POST api/register
// This creates a registration id
public async Task<string> Post(string handle = null)
{
// make sure there are no existing registrations for this push handle (used for iOS and Android)
string newRegistrationId = null;
if (handle != null)
{
var registrations = await hub.GetRegistrationsByChannelAsync(handle, 100);
foreach (RegistrationDescription registration in registrations)
{
if (newRegistrationId == null)
{
newRegistrationId = registration.RegistrationId;
}
else
{
await hub.DeleteRegistrationAsync(registration);
}
}
}
if (newRegistrationId == null) newRegistrationId = await hub.CreateRegistrationIdAsync();
return newRegistrationId;
}使用Google Cloud Messaging,您似乎可以有多个正在工作的GCM注册it,因此您必须处理这个问题。GCM有一个叫做“规范ID”的东西:
如果应用程序中的错误触发同一设备的多个注册,则很难调和状态,您可能会收到重复的消息。 GCM提供了一个名为“规范注册ID”的工具,可以轻松地从这些情况中恢复过来。规范注册ID被定义为应用程序请求的最后一次注册的ID。这是服务器在向设备发送消息时应该使用的ID。 如果稍后尝试使用不同的注册ID发送消息,GCM将像往常一样处理请求,但它将在响应的registration_id字段中包含规范的注册ID。确保将存储在服务器中的注册ID替换为此规范ID,因为您使用的ID最终将停止工作。
https://stackoverflow.com/questions/28967085
复制相似问题