$ionicPush onNotification


#1

I recently upgraded my Push service to use ionic-cloud. Before using this I had been doing the following to setup the requisite handlers in the init method of the $ionicPush service:

                $ionicPush.init({
                    "onNotification": function (notification) {
                        var payload = notification.payload;
                        console.log("onNotification payload", payload);
                        return true;
                    },
                    "onRegister": function (data) {
                        console.log("onRegister, token", data.token);
                        $ionicPush.saveToken(data.token);
                    }
                });
                $ionicPush.register();

Now I have decoupled it as follows which works nicely except I have not been able to find the right way to insert the handler for when the user clicks the notification:

$ionicPush.register().then(
function (t) {
console.log(“register, t”, t);
$ionicPush.saveToken(t).then(
function (t) {
console.log(‘Token saved:’, t.token);
}
)
},
function (error) {
console.error(“register”, error);
});

In short, I am looking to handle the onNotification handler in the new logic.

I tried (per the docs):
$scope.$on(‘cloud:push:notification’, function (event, data) {
console.debug(“data”, data);
});

AND

$ionicEventEmitter.on(‘push:notification’, function(notification) {
console.debug(“notification”, notification);
return true;
});

which lead to dead ends.

Anyone able to assist?

Thanks!


#2

Thought I would answer my own question for others to maybe benefit from. Listen on ‘cloud:push:notification’. $ionicEventEmitter which I saw in various places throughout the forum turned out to be an unproductive rat hole for me.


#3

Hi there!

So what ended up being the issue for you with using cloud:push:notification in the first place? It sounds like you tried that but it didn’t work at first.

The reason I’m asking is that I’m having a similar issue with listening on cloud:push:notification where it works fine if the app is open, but when a user clicks on a notification while the app is closed the watcher doesn’t fire. Any help would be much appreciated.

Thanks!


#4

Hi, I looked through the source code and I saw where and how the event was being sent. I then revisited my code and found that the setup of my handler was happening at a place in my Factory where it was not be invoked (doh! I wasn’t going to mention that; however, since you asked ). This does not sound like your issue anyways.

Not sure what you mean by open/closed. If you mean foreground/background and if you want every notification to come through your handler on arrival (vs. user tap action) then you should look into ‘content-available’. On the other hand, if you literally mean “open/close” then I do not think that is possible for the app to process a notification while it is closed.

BTW I used to use ‘content-available’ except I had a bug where around 30 seconds after running the app would crash. I posted some questions, reached out to Forum, and Slack and never got a response so I rewrote my server-side and app to not depend on this. I imagine that this may have been something in my environment that I was unable to locate.