用户可以有多个设备,其中推送消息将被传递到具有相同标签的所有设备。
每次用户卸载并重新安装该应用程序时,都会返回一个新 token 。因此,对于给定的标签,进行了多次注册,导致重复的推送被传递到同一设备。
azure notification hubs - app uninstall
下面是我们用来注册的代码。
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);
}
最佳答案
据我了解,Apple Push Notification Service
一次只有一个工作的设备 token (另请参阅here),因此在iOS下,一个设备的多个有效设备 token 不会有问题,但是您可以注册多个Azure Notification Hub
一个设备 token 。为了避免这种情况,您必须检查具体设备 token 是否已经注册,如果已经注册,请重新使用并清理它们:
ASP.NET WebAPI-Backend example:
// 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注册ID,因此您必须注意这一点。 GCM有一个叫做“ Canonical IDs
”的东西:关于azure - 使用Azure推送通知重复推送通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28967085/