THE SITUATION:
I am using Ionic Push notifications in my app.
Everything is working fine.
When the user send a message, the notification will be fired through a cURL request, and it will arrive only if the receiver has the app off or in background.
I have just a problem handling the onClick callback.
Following the documentation I have put the callback function in the onNotification parameter.
"onNotification": This function will be called when your device receives a notification, and provided with the notification object received.
What I need to do, when the user click on the notification, is to redirect him to a certain page.
It is working.
The problem is that when the app is in FOREGROUND, the user will be redirected to that page everytime a cURL request is made... regardless of the fact that the notification has been received (app off or background) or not (app foreground)...
THE CODE:
.run(function($ionicPlatform, $rootScope, $location, $state) {
$ionicPlatform.ready(function() {
var push = new Ionic.Push({
"debug": true,
"onNotification": function(notification)
{
$rootScope.get_my_account_data();
$location.path('app/chat');
},
"onRegister": function(data)
{
console.log(data.token);
},
});
THE QUESTION:
How can I manage to setup a callback function that will be called ONLY when the user actually click on the notification, and not when he is inside the app?
I have to do some tricks inside the onNotification or there is a possibility to setup a onClick callback?
Thank you!
THE SOLUTION:
Ok I managed it using the AppStatus
.
By checking it, I will apply the redirecting only if the status of the app is asleep
or closed
.
if (notification.app.asleep || notification.app.closed)
THE CODE:
var push = new Ionic.Push({
"debug": true,
"onNotification": function(notification)
{
$rootScope.get_my_account_data();
if (notification.app.asleep || notification.app.closed)
{
// the app was asleep or was closed, so do the redirect
$location.path('app/your_chat_page');
}
},
"onRegister": function(data)
{
console.log(data.token);
},
});
EDIT:
Now ionic-platform-web-client
is deprecated. You should migrate to Ionic Cloud.
With Ionic Cloud few things changes, so now the code will be slightly different:
$scope.$on('cloud:push:notification', function(event, data)
{
var msg = data.message;
if (msg.app.asleep || msg.app.closed)
{
// the app was asleep or was closed, so do the redirect
$location.path('app/your_chat_page');
}
});
i am working with this method and it is working
1.first initialize the push
var push = PushNotification.init({
android: {
senderID: "XXXXXXXXX"
},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
},
ios: {
alert: "true",
badge: true,
sound: 'true'
},
windows: {}
});
2.push.on('notification', function(data) {
use this for the requested pushes and handle them like
if (data.additionalData.link_url.indexOf('http')>-1){
$state.go('x',{x:x});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With