首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Azure推送通知重复推送通知

使用Azure推送通知重复推送通知
EN

Stack Overflow用户
提问于 2015-03-10 15:00:33
回答 1查看 2.4K关注 0票数 6
  • 我们正在使用为iOS和Android实现推送通知系统。
  • App每次在应用程序启动时都会注册。设备注册为推送通知,标记由appname_userid标识。例如,Android_1122,其中1122是唯一的用户id。在iPhone设备中,iPhone_1122也是相同的。用户可以有多个设备,其中推送消息将被传送到所有具有相同标记的设备。
  • 然而,在为少数用户传递重复推送通知方面,我们面临着一个问题。每次用户卸载和重新安装应用程序时,都会返回一个新的令牌。因此,对于给定的标签,多个注册导致重复推送到同一设备。
  • 也经历过类似的链接,如下所示。但是,不完全清楚使用返回registrationId而不实际创建注册意味着什么。azure通知中心-应用卸载
  • 请提供一些方法,以避免重复注册同一设备。

下面是我们用来注册的代码。

iOS器件

代码语言:javascript
复制
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设备

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2015-03-13 10:29:18

每次用户卸载和重新安装应用程序时,都会返回一个新的令牌。因此,对于给定的标签,多个注册导致重复推送到同一设备。

据我所知,Apple Push Notification Service一次只有一个可用的设备令牌(也请参阅这里),因此在iOS下一个设备的多个有效设备令牌不会出现问题,但是您可以为一个设备令牌进行多个Azure Notification Hub注册。为了避免这种情况,您必须检查是否已经有具体设备令牌的注册,如果是的话,重用并清理它们:

ASP.NET WebAPI-后端示例

代码语言:javascript
复制
// 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注册it,因此您必须处理这个问题。GCM有一个叫做“规范ID”的东西:

如果应用程序中的错误触发同一设备的多个注册,则很难调和状态,您可能会收到重复的消息。 GCM提供了一个名为“规范注册ID”的工具,可以轻松地从这些情况中恢复过来。规范注册ID被定义为应用程序请求的最后一次注册的ID。这是服务器在向设备发送消息时应该使用的ID。 如果稍后尝试使用不同的注册ID发送消息,GCM将像往常一样处理请求,但它将在响应的registration_id字段中包含规范的注册ID。确保将存储在服务器中的注册ID替换为此规范ID,因为您使用的ID最终将停止工作。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28967085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档