I am using push notification in ionic 2 app with below code
import { Push, PushToken } from '@ionic/cloud-angular';
@Component({...})
export MyPage {
constructor(public platform: Platform, public menu: MenuController, public push: Push){
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
if (this.push) {
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
console.log('Token saved:', t.token);
window.localStorage.setItem("deviceToken", t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
console.log('notification msg', msg);
});
}
}
}
}
when i run on device it works fine. but i do ionic serve it gives below error because of injection of Push in constructor
error_handler.js:53 TypeError: platform.toLowerCase is not a function
at Insights.normalizeDevicePlatform (http://localhost:8100/build/main.js:70460:25)
at Insights.markActive (http://localhost:8100/build/main.js:70450:33)
at Insights.checkActivity (http://localhost:8100/build/main.js:70439:22)
at http://localhost:8100/build/main.js:70415:27
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9723)
at Object.onInvokeTask (http://localhost:8100/build/main.js:41825:37)
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9659)
at e.runTask (http://localhost:8100/build/polyfills.js:3:7083)
at invoke (http://localhost:8100/build/polyfills.js:3:10836)
at e.args.(anonymous function) (http://localhost:8100/build/polyfills.js:2:30123)
ErrorHandler.handleError @ error_handler.js:53
As Manoj Rejinthala commented, Push Notifications will not work in the browser (ionic serve
). However, other parts of tour application should run correctly in the browser with ionic serve
.
The error that you describe is a problem with Ionic Cloud. Check if you have the line "@ionic/cloud-angular":"^ 0.9.0"
in the package.json
file, inside dependencies
.
If so, switch it to ^0.9.1
, save the file and run npm install
in the project root folder. This should update @ionic/cloud-angular
to 0.9.1
and its @ionic/cloud
dependency to 0.15.1
.
Then run ionic serve
and it should work.