检测React Native iOS应用程序是否通过推送通知打开
发布时间:2020-12-15 20:52:17 所属栏目:百科 来源:网络整理
导读:Detect if the app was launched/opened from a push notification描述了如何通过用户点击推送通知来检测是否打开了原生iOS应用程序(即启动或仅仅是激活的). 如何在React Native中做同样的事情? PushNotificationIOS 让我附上一个通知侦听器… PushNotifica
Detect if the app was launched/opened from a push notification描述了如何通过用户点击推送通知来检测是否打开了原生iOS应用程序(即启动或仅仅是激活的).
如何在React Native中做同样的事情? PushNotificationIOS.addEventListener('notification',function (notification) { console.log('got a notification',notification); }); 但是当在前台接收到应用程序的推送通知时,以及当我通过推送通知打开应用程序时,这会触发. 特别是如何检测第二种情况?
这里有两种情况需要以不同的方式进行检测:
>应用程序已完全终止(例如重新启动手机,或双击家中,并从后台运行的应用程序列表中将其滑动),并由用户点击推送通知启动.这可以通过 处理这两种情况的代码(您可以将其放在index.ios.js或应用程序启动时运行的其他位置): import React from 'react-native'; var {PushNotificationIOS,AppStateIOS} = React; function appOpenedByNotificationTap(notification) { // This is your handler. The tapped notification gets passed in here. // Do whatever you like with it. console.log(notification); } PushNotificationIOS.getInitialNotification().then(function (notification) { if (notification != null) { appOpenedByNotificationTap(notification); } }); let backgroundNotification; PushNotificationIOS.addEventListener('notification',function (notification) { if (React.AppStateIOS.currentState === 'background') { backgroundNotification = notification; } }); React.AppStateIOS.addEventListener('change',function (new_state) { if (new_state === 'active' && backgroundNotification != null) { appOpenedByNotificationTap(backgroundNotification); backgroundNotification = null; } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |