0

I successfully set up a push notification with Ionic and ios. But I want people to click the notification and go to a dynamically pass in state. And I can not figure out how to do it... I am using a node server. First, my state step up is below:

.state('app.task', {
url: "/task/:taskId",
cache: false,
views: {
  'menuContent': {
    templateUrl: "js/tasks/task-detail.html",
    controller: "browseCtrl",
  }
},
}
})

And in my node server, I pass in the payload as describe here: Ionic.io Push FAQ

var payload = {"$state":"app.task", "$stateParams": "{\"taskId\": \""+ message.relatedTask +"\"}"};
request({
    url: "https://push.ionic.io/api/v1/push",
    method: "POST",
    json: true,
    body: {
      "tokens": [token],
      "notification": {
        "alert": message.message,
        "ios": {
          "badge":1,
          "payload": payload
        },
        "android": {
          "payload": payload
        }
      }
    },
    headers: {
      'Authorization': 'Basic ' + btoa(IONIC_PRIVATE_API_KEY + ":"),
      'X-Ionic-Application-Id': IONIC_APP_ID
    }
  }, function (error, response, body) {
    console.log(body);
  })

And when it click the notification, it won't go to the state (just bring me back to the app). The message.relatedTask is logged as the current taskId. As I can see from XCode console, it logs this:

2015-09-30 18:12:16.488 VideoHappy[1236:403171] Msg: {"$state":"app.task","$stateParams":"{\"taskId\": \"-JzvWR67jIqyfc9JhDMb\"}","$state":"app.task","$stateParams":"{\"taskId\": \"-JzvWR67jIqyfc9JhDMb\"}","badge":"1","body":"Test Title",foreground:"0"}

2015-09-30 18:12:16.600 VideoHappy[1236:403171] $ionicPush:RECEIVED {"$state":"app.task","$stateParams":"{\"taskId\": \"-JzvWR67jIqyfc9JhDMb\"}","badge":"1","body":"Test Title","foreground":"0"}

What did I do wrong?

1
  • Sorry. I take it back what I said and I delete my comment. Thank you for your help!
    – Hugh Hou
    Oct 1, 2015 at 6:56

1 Answer 1

3

The problem turn out to be where I place my $ionicPush.register as below:

$ionicPush.register({
    canShowAlert: false, //Can pushes show an alert on your screen?
    canSetBadge: true, //Can pushes update app icon badges?
    canPlaySound: true, //Can notifications play a sound?
    canRunActionsOnWake: true, //Can run actions outside the app,
    onNotification: function(notification) {
      // Handle new push notifications here
      console.log(notification);
      return true;
    }
  });

I place this code when users signup or login. So the onNotification actual won't run when the notification come in. Instead, I put this code in app.run of my app.js file as follow:

.run(function($ionicPlatform, $ionicPush, $state) {
    $ionicPlatform.ready(function() {
        [go here instead]
    });
})

The key is that you have to ensure your application always runs the push registration call, not just signup or login or clicking a button like the Ionic demo does. I find this out via here (credit: Raymond Camden): http://www.raymondcamden.com/2015/07/02/ionic-push-example-supporting-state-changes

Read that post too for other issues you might want to know before you put up your Ionic Push notification :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.