We are implementing Push Notification System for iOS & Android using Azure Notification Hub.
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.
However there is an issue that we are facing on duplicate push notifications getting delivered for a few users. Every time a user uninstall & 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.
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. azure notification hubs - app uninstall
Please provide some way to avoid duplicate registrations for the same device.
Below is the code we are using to register.
iOS Devices
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 Devices
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);
}