In Ionic 2, I’m trying to handle an event when the user is currently in the application so that I can pop an alert message and navigate to a different view. In my App constructor, I have the following code:
export class MyApp {
private rootPage: any;
constructor(private platform: Platform, private app: App, private push: Push) {
if (UserService.isLoggedIn()) { this.rootPage = PickupPage; }
else { this.rootPage = LoginPage; }
console.log('We are here! We are here! We are here!');
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
console.log('Registering notifications handler');
this.push.rx.notification().subscribe(msg => {
alert(msg.title);
console.log('Got something!');
console.dir(msg);
});
StatusBar.styleDefault();
});
}
}
When I send a notification, on Android I get the notification coming up in the pull down bar on Android, but no console or alert inside the app and on iOS I just get nothing. No console message or alert and no notification in the notification center.
§ ionic -v
2.0.0-beta.37
See if this helps Ionic Push Notification - Android and iOS working
If you follow this tutorial, it will show alert popup when app is open to let use view or ignore the notification.
Thank you. Before I run through the tutorial, I’m trying to understand why the documentation for cloud push notifications flat out doesn’t work (http://docs.ionic.io/services/push/#handling-notifications). On that doc they don’t mention using the IonicNative
Push
object at all.
Edit
Okay. It seems like it does work, so long as I pass senderID
in my cloudSettings config under the android
object instead of just under the push
object:
const cloudSettings: CloudSettings = {
'core': { 'app_id': '***' },
'push': {
'sender_id': '***',
'pluginConfig': {
'ios': { 'badge': true, 'sound': true },
'android': { 'senderID': '***', 'iconColor': '#d1b561' }
}
}
}
It also seems like cloudSettings is very sensitive and doesn’t like to pickup changes after the app is installed on a device.
Edit 2
Okay, I think I have a better understanding of things. So it seems that push notifications work only after I call push.register().then(...)
, which currently my app calls after login. When I add the following code to the app constructor, things seem to work:
this.push.register().then((token: PushToken) => {
return this.push.saveToken(token)
});
```
Now the part that I'm not 100% sure on how to do things properly. When the user logs into my app, which is not driven by Ionic Auth, I want to save the token to my user session on the server. Would I use something like the following code:
```
this.push.register().then((token: PushToken) => {
return this.push.saveToken(token)
}).then((token: PushToken) => {
return this.userService.addDeviceToken(token);
});
```
Or is there a way to just access the already registered token?