检测应用程序是否通过推送通知启动/打开,描述了如何通过用户点击推送通知来检测本机iOS应用程序是否已打开(即启动或仅启动).
我如何在React Native中做同样的事情?PushNotificationIOS
让我附上通知听众......
PushNotificationIOS.addEventListener('notification', function (notification) { console.log('got a notification', notification); });
但是当应用程序在前台收到推送通知时,以及当我通过推送通知打开应用程序时,这都会触发.
如何特别检测第二种情况?
这里有两种情况需要以不同的方式检测:
该应用程序已被完全终止(例如,通过重新启动手机,或者通过双攻的家,然后滑动它关闭在后台运行的应用程序的列表),并正在推出由用户的水龙头上的推送通知.这可以通过该React.PushNotificationIOS.getInitialNotification
方法检测(并获取通知的数据).
应用程序已被暂停,并且用户点击推送通知后再次激活该应用程序.只是像在本机应用程序,你可以告诉大家,这种情况正在发生,因为iOS的通过抽头通知您的应用程序时,它被打开(即使它是一个老的通知),使您的通知处理程序,以火,当你的应用程序是在UIApplicationStateInactive
状态(或'background'
state,正如React Native的AppStateIOS
类所称的那样).
用于处理这两种情况的代码(您可以将其放在您index.ios.js
或在应用程序启动时运行的其他位置):
import React from 'react'; import { PushNotificationIOS, AppState } from 'react-native'; 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 (AppState.currentState === 'background') { backgroundNotification = notification; } }); AppState.addEventListener('change', function (new_state) { if (new_state === 'active' && backgroundNotification != null) { appOpenedByNotificationTap(backgroundNotification); backgroundNotification = null; } });
getInitialNotification
不适用于本地通知.不幸的是,从React Native的0.28开始,using 在本地推送通知启动时PushNotificationIOS.getInitialNotification()
总是返回一个null
值.
正因为如此,你需要捕捉推送通知作为launchOption
你的AppDelegate.m
并把它传递到本地做出反应作为appProperty
.
以下是从冷启动或后台/非活动状态接收本地推送通知所需的全部内容.
AppDelegate.m
(原生iOS代码)
// Inside of your didFinishLaunchingWithOptions method... // Create a Mutable Dictionary to hold the appProperties to pass to React Native. NSMutableDictionary *appProperties = [NSMutableDictionary dictionary]; if (launchOptions != nil) { // Get Local Notification used to launch application. UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (notification) { // Instead of passing the entire Notification, we'll pass the userInfo, // where a Record ID could be stored, for example. NSDictionary *notificationUserInfo = [notification userInfo]; [ appProperties setObject:notificationUserInfo forKey:@"initialNotificationUserInfo" ]; } } // Your RCTRootView stuff... rootView.appProperties = appProperties;
index.ios.js
(React Native)
componentDidMount() { if (this.props.initialNotificationUserInfo) { console.log("Launched from Notification from Cold State"); // This is where you could get a Record ID from this.props.initialNotificationUserInfo // and redirect to the appropriate page, for example. } PushNotificationIOS.addEventListener('localNotification', this._onLocalNotification); } componentWillUnmount() { PushNotificationIOS.removeEventListener('localNotification', this._onLocalNotification); } _onLocalNotification( notification ) { if (AppState.currentState != 'active') { console.log("Launched from Notification from Background or Inactive state."); } else { console.log("Not Launched from Notification"); } }
确保导入PushNotificationIOS
和AppState
从react-native
.
我还没有使用远程推送通知进行测试.也许@MarkAmery的方法可以很好地使用远程推送通知,但不幸的是,从React Native的当前状态来看,这是我能够从冷状态工作获得本地推送通知的唯一方法.
这在React Native中是高度无证的,所以我在他们的GitHub repo上创建了一个问题,以引起对它的注意,并希望纠正它.如果您正在处理此问题,请转到此处并竖起大拇指,使其渗透到顶部.
https://github.com/facebook/react-native/issues/8580