Repeat Push Notification Using Azure Push Notification

  • We are introducing the Push Notification System for iOS and Android using the Azure Notification Hub.

  • Applications are registered every time the application starts. Devices register for push notifications with tags identified by appname_userid. E.g. Android_1122, where 1122 is the unique identifier of the user. The same thing on the iPhone will be iPhone_1122. A user can have several devices where the push message will be intended for delivery to all devices that have the same tag.

  • However, there is a problem that we encounter when duplicating push notifications delivered to multiple users. Each time the user uninstalls and reinstalls the application, a new token is returned. Thus, for this tag, multiple registrations are created, leading to duplication of push transmitted to the same device.

  • Similar links like the ones below have also passed. But it is not entirely clear what exactly is meant when using the REST API to create a registration identifier, which returns a registration without actually creating a registration. notification houses - uninstall application

  • Please indicate a way to avoid duplicate registrations for the same device.

Below is the code we use 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); } 
+6
source share
1 answer

Each time the user uninstalls and reinstalls the application, a new token is returned. So, for this tag, multiple registrations are created, which leads to duplication of pushers delivered to the same device.

As I understand it, there is only one working device token at a time Apple Push Notification Service (also see here ), so you will not have a problem with multiple valid device tokens for a single iOS device, but you can have multiple Azure Notification Hub registrations for one device token. To avoid this, you should check if there is already a registration for a specific device token, and if so, reuse and cleaning:

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; } 

With Google Cloud Messaging , it looks like you can have several working GCM registration identifiers, so you need to take care of this. GCM has something called Canonical IDs ":

If an application error causes multiple registrations for the same device, it can be difficult to reconcile the state and you can duplicate messages.

GCM provides a facility called "canonical registration identifiers" easily from these situations. A canonical registration identifier is defined to be the identifier of the last registration requested by your application. This is the identifier that the server should use when sending messages to the device.

If you later try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration identifier in the response_ registration field. Be sure to replace the registration identifier stored in your server with this canonical identifier, because in the end the identifier you use will stop working.

+3
source

Source: https://habr.com/ru/post/983599/


All Articles