简体   繁体   English

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

[英]Ionic Push notification: How to handle onClick?

THE SITUATION: 情况:

I am using Ionic Push notifications in my app. 我在我的应用程序中使用Ionic Push通知

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. 当用户发送消息时,通知将通过cURL请求触发,并且仅当接收者关闭app或后台时才会到达。

I have just a problem handling the onClick callback. 我只是处理onClick回调的问题。

Following the documentation I have put the callback function in the onNotification parameter. 根据文档,我将回调函数放在onNotification参数中。

"onNotification": This function will be called when your device receives a notification, and provided with the notification object received. “onNotification”:当您的设备收到通知并且收到通知对象时,将调用此函数。

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

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? 我必须在onNotification中做一些技巧,或者有可能设置onClick回调?

Thank you! 谢谢!

THE SOLUTION: 解决方案:

Ok I managed it using the AppStatus . 好的,我使用AppStatus进行了AppStatus By checking it, I will apply the redirecting only if the status of the app is asleep or closed . 通过检查,我将仅在应用程序的状态为asleepclosed应用重定向。

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. 现在,不推荐使用ionic-platform-web-client You should migrate to Ionic Cloud . 您应该迁移到Ionic Cloud

With Ionic Cloud few things changes, so now the code will be slightly different: 使用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');
    }
});

i am working with this method and it is working 我正在使用这种方法,它正在工作

1.first initialize the push 1.首先初始化推送

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 2. push.on('notification', function(data) {用于请求推送和处理它们

if (data.additionalData.link_url.indexOf('http')>-1){
$state.go('x',{x:x});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM