2、cordova plugin add cordova-plugin-whitelist
3、src/index.html
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-eval' 'unsafe-inline' *; object-src 'self'; style-src 'self' 'unsafe-inline'; media-src *">
4、ionic serve
====if success=====
5、home.ts
add
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
add constructor(private http: Http
sendnotify_devicetoken(devictoken) {
let url = 'https://fcm.googleapis.com/fcm/send';
let headers = new Headers({
'Content-Type': 'application/json',
'Authorization': 'key=your key'
});
let options = new RequestOptions({ headers: headers });
let body = {
"notification":{
"title":"Notification title",
"body":"Notification body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
"icon":"fcm_push_icon"
},
"data":{
//"param1":"value1",
//"param2":"value2"
},
//"to":"/topics/topicExample",
"to": devictoken,
"priority":"high",
"restricted_package_name":""
};
this.http.post(url, body, options).map(
(res) => {return res}//res.json()
).subscribe(
data => {
alert(data);
},
err => {
alert("ERROR!: "+err);
}
);
a. devictoken: see http://sueboy.blogspot.tw/2017/01/ionic2-fcm-get-device-token.html
b. your key:firebase console -> project setting -> cloud messaging ->
have two new and old api key. Use new is better
6、ionic serve push button, then send success