@ibowankenobi
You have to follow this http://docs.ionic.io/services/push/, to generate all the keys and Ids necessary.
Then in your *.ts code:
At the startup of the app you have to register the token of each user and save it on custom db (the example call a php Page http:///indexToken.php):
registerToken(){
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
//salvo a db il token
let variables = 'pushtoken=' + t.token + '&name=' + this.username;
let headers = new Headers();
let serveraddress = 'http://<yourwww>/indexToken.php';
//content-type
headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
/**************************************/
// don't have the data yet
new Promise(resolve => {
this.http.post(serveraddress,variables,{headers:headers})
.map(
res => res.json()
)
.subscribe(data => {
alert('Token saved DB: ' + data['ret']);
},
err =>{
alert('error token DB: ' + err);
});
});
alert('Token saved: '+ t.token);
});
}
Then you can retrieve the tokens of the users from db, I use an auxiliar User class. Then you should be able to send push notification, setting the specific user token in the call.
export class User {
id: string;
name:string;
pushtoken:string;
constructor(name: string,pushtoken:string) {
this.pushtoken = pushtoken;
this.name = name;
}
}
sendPushByPhone(event, user) {
var token = user.pushtoken;
//PROFILE_TAG The Security Profile tag found in Settings › Certificates in the Dashboard
// my profile tag is named "prod"
let body: any = {
"tokens": [token],
"send_to_all": false,
"profile": "prod",
"notification": {
"message": "Test From App"
}
}
//generate apitoken https://docs.ionic.io/api/http.html
var apitoken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization','Bearer ' + apitoken);
return this.http.post("https://api.ionic.io/push/notifications"
, body, { headers: headers })
.map((response) => response)
.subscribe(data => {
},
err =>{
alert('error token DB: ' + err);
}
)
}