gpt4 book ai didi

azure - 使用 Azure 推送通知复制推送通知

转载 作者:行者123 更新时间:2023-12-04 15:33:30 24 4
gpt4 key购买 nike

  • 我们正在使用 Azure 通知中心实现适用于 iOS 和 Android 的推送通知系统。

  • 应用程序每次启动时都会注册。设备通过 appname_userid 标识的标签注册推送通知。例如Android_1122 其中 1122 是唯一的用户 ID。 iPhone 设备中的相同内容为 iPhone_1122。用户可以拥有多个设备,其中推送消息将被传递到具有相同标签的所有设备。

  • 但是,我们面临着向少数用户发送重复推送通知的问题。每次用户卸载并重新安装应用程序时,都会返回一个新 token 。因此,对于给定的标签,进行多次注册会导致重复推送到同一设备。

  • 也浏览过类似的链接,如下所示。但是,并不完全清楚使用创建注册 ID REST API(该 API 返回注册 ID 而不实际创建注册)的确切含义。 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);
}

最佳答案

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.

据我了解,Apple 推送通知服务一次只有一个工作设备 token (另请参阅 here ),因此您不会遇到多个有效设备 token 的问题在 iOS 下,一台设备可以使用多个设备 token ,但一个设备 token 可以有多个 Azure 通知中心 注册。为了避免这种情况,您必须检查是否已经有具体设备 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 ”的东西:

If a bug in the app triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

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

If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.

关于azure - 使用 Azure 推送通知复制推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28967085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com