离子推送通知:如何处理onClick?

Fra*_*ssi 12 javascript android push-notification ios ionic-framework

情况:

我在我的应用程序中使用Ionic Push通知.

一切都很好.

当用户发送消息时,通知将通过cURL请求触发,并且仅当接收者关闭app或后台时才会到达.

我只是处理onClick回调的问题.

根据文档,我将回调函数放在onNotification参数中.

"onNotification":当您的设备收到通知并且收到通知对象时,将调用此函数.

当用户点击通知时,我需要做的是将他重定向到某个页面.

这是工作.

问题是,当应用程序处于FOREGROUND时,每次发出cURL请求时,用户都将被重定向到该页面...无论是否已收到通知(应用程序关闭或后台)(应用程序前景) ...

代码:

.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);
        },

    });
Run Code Online (Sandbox Code Playgroud)

问题:

如何设置一个回调函数,该函数仅在用户实际点击通知时被调用,而不是在他进入应用程序时?

我必须在onNotification中做一些技巧,或者有可能设置onClick回调?

谢谢!

Fra*_*ssi 6

解决方案:

好吧,我用它来管理它AppStatus.通过检查,我将仅在应用程序的状态为asleep或时应用重定向closed.

if (notification.app.asleep || notification.app.closed)

代码:

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

    });
Run Code Online (Sandbox Code Playgroud)

编辑:

现在ionic-platform-web-client已弃用.您应该迁移到Ionic Cloud.

使用Ionic Cloud时,几乎没有什么变化,所以现在代码会略有不同:

$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');
    }
});
Run Code Online (Sandbox Code Playgroud)