2017-08-14 53 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

相關問題