2014-09-25 10 views
6

Sto usando Parse e non riesco a far funzionare questo metodo.Parse Cloud: Invia Push a un singolo utente

L'idea è di inviare push a un singolo utente con un identificativo utente già noto da qualsiasi piattaforma all'interno di "codice Cloud".

query Ho provato senza spinta essere consegnati per l'utente (in realtà alcuna spinta è stato consegnato a tutti gli utenti)

  • PFInstallation attraverso il puntatore utente:

    var targetUser = new Parse.User();

    targetUser.id = userID;

    var query = new Parse.Query(Parse.Installation);

    query.equalTo('user', targetUser);

  • query in PFUser sé

    var query = new Parse.Query(Parse.User);

    query.equalTo('objectId', userID);

codice completo per il metodo:

Parse.Cloud.define("sendPushContactUser", function(request, response) { 

// Get parameters 
var userID  = request.params.userID; // I'm obviously passing this parameter in the method call 


// Query 
/* ------ query = Any of the above options ------ */ 


Parse.Push.send({ 
    where: query, 
    data: { 
     title : "New message", 
     alert : "User message", 
     badge : "Increment", 
     sound : "soundNotifUserContact.caf", 
     "params" : { 
      "pushType" : 2, 
      "userID" : userID 
     } 
    } 
}, { 
    success: function() { 
     response.success(); 
    }, 
    error: function(error) { 
     response.error(error); 
    } 
    }); 

}); 

Grazie per il tuo tempo e il tuo aiuto.

+0

come Chiamate il metodo cloud da Android ?? – Erum

risposta

10

Questo funziona bene per me, ma è necessario collegare gli utenti con le installazioni prima:

var query = new Parse.Query(Parse.User); 
query.equalTo('username', 'Sento'); 
// Find devices associated with these users 
var pushQuery = new Parse.Query(Parse.Installation); 
// need to have users linked to installations 
pushQuery.matchesQuery('user', query); 


Parse.Push.send({ 
    where: pushQuery, 
    data: { 
     aps: { 
      alert: "Test", 
      sound: "" 
     } 
    } 
}, { 
    success: function() { 
     response.success("Hello world!"); 
    }, 
    error: function (error) { 
     response.error(error); 
    } 
}); 
+0

Grazie per la risposta – Sento

+0

Ho aggiunto un puntatore a User.installation e il collegamento sembra soddisfacente. Sono in grado di fare clic sull'installazione e l'installazione è stata visualizzata. Ma ancora incapace di inviarlo. –

+0

Sì! Benedici il tuo cuore per questo. – darkheartfelt

-2

Sto utilizzando il primo dei due metodi che hai menzionato e funziona correttamente.

Assicurati di utilizzare objectId e non il nome utente per targetUser.id.

Es:

targetUser.id = 'Qhao1j22j'; //Never hard code like this though. 
0

È possibile aderire Parse.Session e Parse.Installation con un puntatore dell'utente come chiave primaria Session, next "installationId" come chiave join.

var PushSend = function(payload, dbContact) { 
    var querySession = new Parse.Query(Parse.Session); 
     querySession.equalTo("user", dbContact); 
     querySession.descending("updatedAt"); 

querySession.first({ useMasterKey: true }) 

.then(function(dbSession) { 
    var installId = dbSession.get("installationId"); 

    var installQuery = new Parse.Query(Parse.Installation); 
     installQuery.equalTo("installationId", installId); 

     Parse.Push.send({ 
      where: installQuery, 
      data: { 
       alert: payload 
      } 
     }, { useMasterKey: true}) 

     .then(function() { 
      // "success" 
     }, function(error) { 
      // "failed" 
     }); 
    }, function(error) { 
     // "failed" 
    }); 
} 

La funzione nube sopra prende come argomenti:

  1. il testo payload stringa
  2. un puntatore contatto destinazione (si è per esempio in precedenza interrogato da Parse.User)