检测应用程序是否通过推送通知启动/打开,描述了如何通过用户点击推送通知来检测本机iOS应用程序是否已打开(即启动或仅启动).
我如何在React Native中做同样的事情?PushNotificationIOS让我附上通知听众......
PushNotificationIOS.addEventListener('notification', function (notification) {
console.log('got a notification', notification);
});
Run Code Online (Sandbox Code Playgroud)
但是当应用程序在前台收到推送通知时,以及当我通过推送通知打开应用程序时,这都会触发.
如何特别检测第二种情况?
Mar*_*ery 39
这里有两种情况需要以不同的方式检测:
React.PushNotificationIOS.getInitialNotification方法检测(并获取通知的数据).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;
}
});
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
确保导入PushNotificationIOS和AppState从react-native.
我还没有使用远程推送通知进行测试.也许@MarkAmery的方法可以很好地使用远程推送通知,但不幸的是,从React Native的当前状态来看,这是我能够从冷状态工作获得本地推送通知的唯一方法.
这在React Native中是高度无证的,所以我在他们的GitHub repo上创建了一个问题,以引起对它的注意,并希望纠正它.如果您正在处理此问题,请转到此处并竖起大拇指,使其渗透到顶部.
https://github.com/facebook/react-native/issues/8580
| 归档时间: |
|
| 查看次数: |
6886 次 |
| 最近记录: |