gpt4 book ai didi

javascript - ionic 推送通知 : How to handle onClick?

转载 作者:技术小花猫 更新时间:2023-10-29 10:38:04 26 4
gpt4 key购买 nike

情况:

我正在使用 Ionic Push notifications在我的应用程序中。

一切正常。

当用户发送消息时,通知将通过 cURL 请求触发,只有当接收者关闭应用程序或处于后台时,通知才会到达。

我在处理 onClick 回调时遇到了问题。

documentation 之后我已将回调函数放在 onNotification 参数中。

"onNotification": This function will be called when your device receives a notification, and provided with the notification object received.

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

它正在工作。

问题是,当应用程序处于前台时,每次发出 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);
},

});

问题:

我如何设法设置一个仅当用户实际点击通知时才会调用的回调函数,而不是当他在应用程序中时?

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

谢谢!

最佳答案

解决方案:

好的,我使用 AppStatus 管理它。通过检查它,我将仅在应用程序的状态为 asleepclosed 时应用重定向。

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

});

编辑:

现在 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');
}
});

关于javascript - ionic 推送通知 : How to handle onClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39143501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com