【问题标题】:Duplicate Push Notification using Azure Push Notification使用 Azure 推送通知复制推送通知
【发布时间】:2015-05-12 02:09:01
【问题描述】:
  • 我们正在使用 Azure 通知中心为 iOS 和 Android 实施推送通知系统。

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

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

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

【问题讨论】:

    标签: azure apple-push-notifications google-cloud-messaging azure-notificationhub


    【解决方案1】:

    每次用户卸载并重新安装应用程序时,都会生成一个新令牌 回来。因此,对于给定的标签,进行了多次注册 导致向同一设备发送重复推送。

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

    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”的东西:

    如果应用程序中的错误触发相同的多个注册 设备,协调状态可能很困难,您最终可能会得到 重复消息。

    GCM 提供了一种称为“规范注册 ID”的工具,可以轻松地 从这些情况中恢复过来。定义了一个规范的注册 ID 成为您的应用程序请求的最后一次注册的 ID。这是 服务器向设备发送消息时应使用的 ID。

    如果您稍后尝试使用其他注册方式发送消息 ID,GCM 会照常处理请求,但会包含 的registration_id字段中的规范注册ID 回复。确保替换存储在您的注册 ID 具有此规范 ID 的服务器,因为最终您使用的 ID 将 停止工作。

    【讨论】:

    • 完美——你成就了我的一天。不知道 GCM 使用多个工作设备令牌!现在我的推送服务工作了!
    • 但是这段代码会错过在删除和创建之间发送的通知。
    猜你喜欢
    • 2016-04-24
    • 2015-05-06
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多