title: Duplicate Push Notification using Azure Push Notification
body: <ul>
  <li><p>We are implementing Push Notification System for iOS &amp; Android using Azure Notification Hub.</p></li>
  <li><p>App registers every time during app launch. Devices are registered for push notification with tags identified by appname_userid. For e.g. Android_1122 where 1122 is an unique user id. The same in an iPhone device will be iPhone_1122.
  A user can have multiple devices, where a push message will be meant to be delivered to all devices having the same tag.</p></li>
  <li><p>However there is an issue that we are facing on duplicate push notifications getting delivered for a few users. 
  Every time a user uninstall &amp; re-installs the app, a new token is returned. So, for that given tag, multiple registrations are made leading to duplicate pushes delivered to the same device.</p></li>
  <li><p>Have also gone through similar links like the one below. But, not entirely clear as to what is exactly meant by using the Create Registration ID REST API that returns a registrationId without actually creating a registration.
  <a href="https://stackoverflow.com/questions/22564354/azure-notification-hubs-app-uninstall">azure notification hubs - app uninstall</a></p></li>
  <li><p>Please provide some way to avoid duplicate registrations for the same device. </p></li>
  </ul>
  
  <p>Below is the code we are using to register.</p>
  
  <p>iOS Devices</p>
  
  <pre><code>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);
  }];
  </code></pre>
  
  <p>Android Devices</p>
  
  <pre><code>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);
  }
  </code></pre>