2017-08-14 52 views
1

我试图在开发模式中在Azure推送通知中心注册设备令牌,但在Azure门户中,它显示0个已注册的活动设备,当我检查通知中心时将显示一次登记发生。设备未在Azure推送通知集线器中以xamarin格式注册

这里是我的示例代码:

应用程序的委托:

 var deviceTokenDes = deviceToken.Description; 

     if (!string.IsNullOrWhiteSpace(deviceTokenDes)) 
     { 
      deviceTokenDes = deviceTokenDes.Trim('<'); 
      deviceTokenDes = deviceTokenDes.Trim('>'); 
      deviceTokenDes = deviceTokenDes.Replace(" ", ""); 

      DeviceToken = deviceTokenDes.Trim('<'); 
      DeviceToken = deviceTokenDes.Trim('>'); 
      DeviceToken = deviceTokenDes.Replace(" ", ""); 
     } 

     Hub = new SBNotificationHub(myapp.ListenConnectionString, myapp.NotificationHubName); 

登录浏览模式:

var tags = new List<string> { userId }; 

     AppDelegate.Hub?.UnregisterAllAsync(AppDelegate.DeviceToken, error => 
     { 
      if (error != null) 
      { 
       Console.WriteLine("Error calling Unregister: {0}", error); 
      } 

      AppDelegate.Hub.RegisterNativeAsync(AppDelegate.DeviceToken, new NSSet(tags.ToArray()), errorCallback => 
      { 
       if (errorCallback != null) 
       { 
        Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString()); 
       } 
      }); 

我已经注册设备标识与标签后,用户成功登录到应用程序。你能提出建议吗? enter image description here

回答

0

这就是我们在AppDelegate类中所做的一切。

public override async void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) 
     { 
      bool ShouldComplete = true; 

      // Validate if we have already got a registration 
      try 
      { 
       string validation = NSUserDefaults.StandardUserDefaults.StringForKey("InitialTagRegistration"); 
       if (validation.Contains("Completed")) 
       { 
        ShouldComplete = false; 
       } 
      } 
      catch (Exception genEx) 
      { 
       ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx); 
      } 

      Hub = new SBNotificationHub(ConfigurableSettings.NotificationHubConnectionString, ConfigurableSettings.NotificationHubPathName); 

      ApplicationState.SetValue("NotificationHub", Hub); 

      // Get previous device token 
      NSData oldDeviceToken = await ApplicationSettings.RetrieveDeviceToken(); 

      // If the token has changed unregister the old token and save the new token to UserDefaults. 
      if (oldDeviceToken != null) 
      { 
       if (oldDeviceToken.ToString() != deviceToken.ToString()) 
       { 
        try 
        { 
         Hub.UnregisterAllAsync(oldDeviceToken, (error) => 
         { 
          //check for errors in unregistration process. 
          if (error != null) 
          { 
           ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + error + " | Source: " + "Unregistering old device token against the notification hub."); 
           //exit out of the code here because we can't keep our hub clean without being able to remove the device from our registration list. 
           return; 
          } 
          else 
          { 
           ShouldComplete = true; 
          } 
         }); 
        } 
        catch (Exception genEx) 
        { 
         ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx + Environment.NewLine + Environment.NewLine); 
        } 
       } 
      } 
      else 
      { 
       // Store current device token 
       bool res = await ApplicationSettings.CacheDeviceToken(deviceToken); 
      } 

      // Check if we need to perform our initial registrations 

      if (ShouldComplete) 
      { 
       NSSet RegisteredTags = await ApplicationSettings.RetrieveUserTags(); 

       if (RegisteredTags == null) 
       { 
        RegisteredTags = new NSSet("AppleDevice"); 
       } 

       //Register the device against the notification hub keeping the details accurate at all times. 
       Hub.RegisterNativeAsync(deviceToken, RegisteredTags, (errorCallback) => 
       { 
        if (errorCallback != null) 
        { 
         ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + errorCallback + " | Source: " + "Registering device token against the notification hub."); 
        } 
        else 
        { 
         if (deviceToken != null) 
         { 
          NSUserDefaults.StandardUserDefaults.SetString("Completed", "InitialTagRegistration"); 
          NSUserDefaults.StandardUserDefaults.Synchronize(); 
         } 
        } 
       }); 
      } 
     } 

长期和短期的它是你不需要传递到蔚蓝的通知中心之前,做任何事情来的设备令牌。这就是我们解决问题的方法,我们几个月来一直在积极应用中运行,没有任何问题。希望这可以帮助。

编辑:为了在用户登录时更新标签,我们存储了设备令牌供以后使用,当用户登录时,我们在单独的类中使用以下方法以方便更新标签:

public static async Task<bool> UpdateTags(StaffProfile user) 
    { 
     //Get the instance of the Notification hub 
     SBNotificationHub UpdateHub = new SBNotificationHub(ConfigurableSettings.NotificationHubConnectionString, ConfigurableSettings.NotificationHubPathName); 
    //Grab the current device token that was stored during the start up process. 
    NSData CurrentDeviceToken = await ApplicationSettings.RetrieveDeviceToken(); 

    //Get and create the tags we want to use. 

    string EmailTag = string.Empty; 
    string StoreTag = string.Empty; 
    string OrganisationTag = "AppleDevice:OrgTag"; 
    string GenericTag = "AppleDevice:StaffTag"; 

    if (!string.IsNullOrWhiteSpace(user.Email)) 
    { 
     EmailTag = user.Email; 
     //Remove unwanted spaces and symbols. 
     EmailTag = EmailTag.Replace(" ", ""); 
     EmailTag = string.Format("AppleDevice:{0}", EmailTag); 
    } 

    if (!string.IsNullOrWhiteSpace(user.Store?.Name)) 
    { 
     StoreTag = user.Store.Name; 
     //Remove unwanted space. 
     StoreTag = StoreTag.Replace(" ", ""); 
     StoreTag = string.Format("AppleDevice:{0}", StoreTag); 
    } 

    //Create array of strings to the currently fixed size of 3 items. 
    NSString[] TagArray = new NSString[4]; 

    //Only add in the tags that contain data. 
    if (!string.IsNullOrEmpty(EmailTag)) { TagArray[0] = (NSString)EmailTag; } 
    if (!string.IsNullOrEmpty(StoreTag)) { TagArray[1] = (NSString)StoreTag; } 
    if (!string.IsNullOrEmpty(OrganisationTag)) { TagArray[2] = (NSString)OrganisationTag; } 
    if (!string.IsNullOrEmpty(GenericTag)) { TagArray[3] = (NSString)GenericTag; } 

    NSSet tags = new NSSet(TagArray); 

    // Store our tags into settings 
    ApplicationSettings.CacheUserTags(tags); 

    try 
    { 
     if (CurrentDeviceToken == null) 
     { 
      ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: Device token is empty." + Environment.NewLine + Environment.NewLine); 
     } 
     else 
     { 
      UpdateHub.RegisterNativeAsync(CurrentDeviceToken, tags, (error) => 
      { 
       //check for errors in unregistration process. 
       if (error != null) 
       { 
        ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + error + " | Source: " + "Registering against hub with new tags." + Environment.NewLine + Environment.NewLine); 

        // Lets do this so that we can force the initial registration to take place again. 
        NSUserDefaults.StandardUserDefaults.SetString("Failed", "InitialTagRegistration"); 
        NSUserDefaults.StandardUserDefaults.Synchronize(); 
       } 
       else 
       { 
        ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[INFORMATION] - Message: Successful Registration - Source: Registering against hub with new tags." + Environment.NewLine + Environment.NewLine); 
       } 
      }); 
     } 
    } 
    catch (Exception genEx) 
    { 
     ApplicationLog.AppendFile(DateTime.Now.ToString() + " : " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx + Environment.NewLine + Environment.NewLine); 
    } 

    return await Task.FromResult(true); 
} 
+0

Thanks Digitalsa1nt。在我的应用程序中,我想将userId设置为'标记'。所以当用户登录到应用程序时,我们只能得到userId,但上面的代码可以写在应用程序委托中,这些代码在登录到应用程序之前执行。如何在用户登录到应用程序后实现此代码。 – Deepak

+0

因此,以上是我们用来为“设备”特定通知进行初始注册的地方,我们只关心安装了我们的应用的用户(所以关于事件的通用通知等),当用户登录时,我们有一个独立的类,它使用我们上面收到的devicetoken,并使用新标签重新注册设备。 – Digitalsa1nt

+0

我们如何重新注册使用新标签的设备。当我们休耕相同的代码或需要修改代码时。 – Deepak

0

我已经尝试过休耕码但我没有收到任何通知。当我第一次在azure通知中心发送测试通知时,它会显示一次成功,但没有收到通知,第二次之后显示0通过,0次失败。

根据您的描述,我建议您可以直接与APN沟通,以检查是否可以获得错误响应以缩小此问题。下面是一些有用的教程,你可以参考它们并解决您的问题如下:

  • 正式文件:Communicating with APNs

  • Knuff,苹果推送通知服务调试应用程序(的APN)

  • PushSharp,用于向iOS/OSX(APNS),Android/Chrome(GCM),Windows/Windows Phone,Amazon(ADM)和Blackberry设备发送通知的服务器端库。有关如何配置和发送Apple推送通知,请参阅here

相关问题