7

Ho iniziato a utilizzare Servizio app di Azure questo mese, prima che il servizio app stia utilizzando Servizi mobili di Azure e il token del dispositivo di registrazione con i tag sia piuttosto semplice ma nel servizio app mi sono reso conto che è stato rimosso a causa di problemi di sicurezza, quindi devo farlo con l'API personalizzata.Risoluzione dei problemi Servizio app di Azure Registrazione push notfication con tag in iOS

Sto utilizzando un'autenticazione personalizzata (servizio di autenticazione non azzurro (a causa del mio client non lo desidera)) con il mio database in modo che ho bisogno di impostare l'id utente come un tag per inviare la notifica a utente specifico.Tuttavia sono di fronte a problemi che, anche se la registrazione di token dispositivo è OK (posso inviare spinta a tutti senza tag) i tag non funzionano, stavo seguendo questi post del blog

https://blogs.msdn.microsoft.com/writingdata_services/2016/04/14/adding-push-notification-tags-from-a-node-js-backend/

https://blogs.msdn.microsoft.com/writingdata_services/2016/01/22/adding-push-notification-tags-from-an-azure-mobile-apps-client/

My Custom API (updateNotification.js)

var api = { 

    get: (request,response,next) => { 
      var push = request.azureMobile.push; 
      var installationId = request.query.id; 

      push.getInstallation(installationId, function(error, installation, res){ 
       if (error){     
        console.log('An error occurred when retrieving installation : ' + error); 
        response.status(error.statusCode).send(error.detail);     
       } 
       else{ 
        // Return an array of current tags. 
        response.json(installation.tags); 
       }    
      }); 
    }, 

    post: (request, response, next) => { 
     var push = request.azureMobile.push; 
     var installationId = request.query.id; 
     var tags = request.query.userID; 

     var updateOperation = [{ 
      "op": "add", 
      "path": "/tags", 
      "value": tags.toString() 
     }];  

     push.patchInstallation(installationId, updateOperation, function(error, res){ 
      if(error){ 
       console.log(error) 
       response.status(error.statusCode).send(error.detail); 
      } 
      else{ 
       console.log("Success"); 
       console.log("Tags : " + tags); 
       response.status(200).send(tags); 
      } 
     }); 
    } 
}; 

module.exports = api; 

Nella mia classe AppDelegate.swift stavo facendo questo

func application(application: UIApplication, 
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     let keychain = KeychainSwift() 
     let id : NSString = keychain.get("userID")! as String 
     let client = MSClient(applicationURLString: "https://XXXX.XXXX.XXX") 
     var deviceTokenString = "\(deviceToken)" 
     deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "") 
     deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "") 
     deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ", withString: "-")   

     client.push?.registerDeviceToken(deviceToken, completion: { (error) in 
      if let err = error { 
       print("ERROR ", err) 
      }else{ 
       client.invokeAPI("updateNotification", body: nil, HTTPMethod: "Post", parameters: ["id" : deviceTokenString , "userID" : id], headers: nil) { (result, response, error) in 
        if response?.statusCode != 200 { 
         NSLog("ERROR %@", error!) 
        } else { 
         print("Tags Successfully Implemented") 
        } 
       } 
      } 
     }) 

    } 

Tutto sembra bene in questo momento, nella mia console posso vedere il mio userid, il mio dispositivo token e userID è qui in questo modo (io sono Puttin X nel mezzo di esso: D dispiace per quello)

deviceToken = 22afedf6-a08f1ce9-XXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXX-db431577-2dfbbee1 ID utente = 88d06c97-XXXXXXXX-XXXX-042215c46575

Tuttavia quando ho provato questo metodo GET per vedere i tag di dispositivo di id,

client.invokeAPI("updateNotification", body: nil, HTTPMethod: "GET", parameters: ["id" : deviceTokenString], headers: nil) { (result, response, error) in 
      if response?.statusCode != 200 { 
        NSLog("ERROR %@", error!) 
      } else { 
        print(result) 
      } 
    } 

sto ottenendo questo errore:

errore di dominio = Codice com.Microsoft.MicrosoftAzureMobile.ErrorDomain = -1302 " l'installazione non found.TrackingId: 57239dd3-XXXX-XXXX-XXXX-0bd9579c660e_G1, timestamp: 2016/07/18 08:22:05 "

Come faccio a risolvere questo messaggio di errore?

+1

Could segui [la linea guida di diagnostica] (https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-push-notification-fixer/) e se non aiuta, potresti ripro il problema e incollare l'ID di tracciamento completo qui? (Non c'è niente di sensibile a riguardo, è solo una stringa casuale.) –

+0

@NikitaG. ecco il mio ID di tracciamento c5c802bd-c94f-46f7-b87d-bddbe0e277d4_G6 – KBB

risposta

4

OK, ora dopo un lungo lavoro Ecco la soluzione

1.Step

enter image description here

Se è possibile debug the Notification Hub (sto suggerendo di debug da Visual Studio 2015 Express, ecc ..) , puoi vedere che l'identificatore PNS (, ovvero il token del dispositivo) è tutto maiuscolo senza trattini, quindi per prima cosa dobbiamo modificare il codice del token del dispositivo che era nel nostro AppDelegate.swift che sarà come questo

deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "") 
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "")  
deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ",withString: "-") 
deviceTokenString = deviceTokenString.uppercaseString 

//In case of any spaces just guarantee with trim 
let trimmedDeviceToken = deviceTokenString.stringByTrimmingCharactersInSet(
    NSCharacterSet.whitespaceAndNewLineCharacterSet() 
) 

2.Step

InstallationID e dispositivi token sono cose totalmente diverse.Stai spingendo i tag su installationId e non sul token del dispositivo in modo da avere bisogno di installare ID di installazione nel tuo codice client.

Il problema è in Azure Mobile SDK per IOS non è possibile chiamare l'ID di installazione con client.installationId (Almeno ho potuto trovare il modo di ottenere da MSClient Object)

quello che faccio è, vado nel quadro e trovare MSClient.m .E Mi rendo conto che l'ID di installazione è effettivamente memorizzata in NSUserDefaults con chiave "WindowsAzureMobileServicesInstallationId" .Così posso raggiungerlo dal Defaults NSUser con questo

let defaults = NSUserDefaults.standartUserDefaults() 
let installationID = defaults.stringForKey("WindowsAzureMobileServicesInstallationId") 

dopo questi passaggi si può effettivamente registrarsi tag l'installazione n id. Ecco il risultato e il codice completo di applicazione metodo didRegisterForRemoteNotificationsWithDeviceToken

func application(application: UIApplication, 
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
     let keychain = KeychainSwift() 
     let id : NSString = keychain.get("userID")! as String 
     let client = MSClient(applicationURLString: "https://XXXX.XXXX.XXX") 
     var deviceTokenString = "\(deviceToken)" 
     deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString("<", withString: "") 
     deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(">", withString: "") 
     deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ", withString: "") 
     deviceTokenString = deviceTokenString.uppercaseString 

     //In case of any spaces just guarantee with trim 
     let trimmedDeviceToken = deviceTokenString.stringByTrimmingCharactersInSet(
      NSCharacterSet.whitespaceAndNewLineCharacterSet() 
     ) 

     let defaults = NSUserDefaults.standartUserDefaults() 
     let installationID = defaults.stringForKey("WindowsAzureMobileServicesInstallationId") 

     client.push?.registerDeviceToken(deviceToken, completion: { (error) in 
      if let err = error { 
       print("ERROR ", err) 
      }else{ 
       client.invokeAPI("updateNotification", body: nil, HTTPMethod: "Post", parameters: ["id" : installationID! , "userID" : id], headers: nil) { (result, response, error) in 
        if response?.statusCode != 200 { 
         NSLog("ERROR %@", error!) 
        } else { 
         print("Tags Successfully Implemented") 
        } 
       } 
      } 
     }) 

    } 

enter image description here

Dopo questo si può anche controllare i tag di chiamando il metodo GET di updateNotification con l'invio di InstallationID che otteniamo da NSUserDefaults

Problemi correlati