如何在收到Ionic推送通知时打开特定状态?

时间:2015-10-01 01:23:03

标签: node.js push-notification ionic-framework ionic

我成功设置了Ionic和ios的推送通知。但我希望人们点击通知并进入动态传递状态。我无法弄清楚如何做到这一点...... 我正在使用节点服务器。首先,我的状态升级如下:

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

在我的节点服务器中,我传递了有效负载,如下所述: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);
  })

当它点击通知时,它将不会进入状态(只需将我带回应用程序)。 message.relatedTask被记录为当前taskId。正如我从XCode控制台中看到的那样,它记录了这个:

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”}

我做错了什么?

1 个答案:

答案 0 :(得分:3)

问题结果是我将$ ionicPush.register置于以下位置:

$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;
    }
  });

我在用户注册或登录时放置此代码。因此,当通知进入时,onNotification实际上不会运行。相反,我将此代码放在app.js文件的app.run中,如下所示:

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

关键是你必须确保你的应用程序始终运行推送注册调用,而不仅仅是注册或登录或点击像Ionic演示那样的按钮。我通过这里找到了这个(信用:雷蒙德卡姆登):http://www.raymondcamden.com/2015/07/02/ionic-push-example-supporting-state-changes

在您提出Ionic Push通知之前,请阅读该文章以了解您可能想知道的其他问题:)