2015-10-12 69 views
1

首先,“热启动”意味着使用系统推送通知从后台状态启动应用程序。“暖”推出推送通知

我很努力地将我的应用程序路由到适当的屏幕以获得热启动通知。

在冷启动时,通知可通过PushNotificationIOS.popInitialNotification()获得,但在热启动时返回null。回应“通知”事件很困难,因为我无法确定应用程序是从后台启动还是在前台运行时收到通知。

在React Native中处理热启动通知的正确方法是什么?

这里是我的PushNotificationHandler,它封装应用的核心导航:

export default class PushNotificationHandler extends React.Component { 
    constructor(props) { 
    super(props) 

    this.state = { 
     modalInitialRoute: null, 
     modalOpen: false, 
    } 
    } 

    openModalWithRoute(route) { 
    this.setState({ 
     modalInitialRoute: route, 
     modalOpen: true, 
    }) 
    } 

    closeModal() { 
    this.setState({ 
     modalOpen: false, 
    }) 
    } 

    renderModal() { 
    const { 
     modalInitialRoute, 
     modalOpen, 
    } = this.state 

    return (
     <Modal 
     close={() => this.closeModal()} 
     open={modalOpen} 
     initialRoute={modalInitialRoute}/> 
    ) 
    } 

    componentWillMount() { 
    PushNotificationIOS.addEventListener('register', this._onPushNotificationRegistration) 
    PushNotificationIOS.addEventListener('notification', this._onPushNotification) 
    } 

    componentDidMount() { 
    this.requestPushNotificationPermission() 

    const notification = PushNotificationIOS.popInitialNotification() 
    if (notification) { 
     this.displayNotification(notification) 
    } 
    } 

    componentWillUnmount() { 
    PushNotificationIOS.removeEventListener('register', this._onPushNotificationRegistration) 
    PushNotificationIOS.removeEventListener('notification', this._onPushNotification) 
    } 

    requestPushNotificationPermission() { 
    PushNotificationIOS.checkPermissions((permissions) => { 
     // If no permissions are allowed, request permissions. 
     if (!(permissions.alert || permissions.badge || permissions.sound)) { 
     PushNotificationIOS.requestPermissions() 
     } 
    }) 
    } 

    _onPushNotificationRegistration(deviceToken) { 
    const { 
     registerForPushNotifications, 
    } = this.props 

    const userId = this.props.parentId 
    registerForPushNotifications(userId, deviceToken) 
    } 

    _onPushNotification(notification) { 
    const { 
     receivePushNotification, 
    } = this.props 

    receivePushNotification(notification) 
    } 

    displayNotification(notification) { 
    const { 
     receivePushNotification 
    } = this.props 

    const data = notification.getData() 

    switch (data.type) { 
     case WORKSHEET_FEEDBACK: 
     this.openModalWithRoute({ 
      component: SubmissionDetailsScreenContainer, 
      title: 'Submission Details', 
      key: 'submissionDetails', 
      props: { 
      submissionId: data.sID, 
      }, 
     }) 
     default: 
     break 
    } 
    } 

    render() { 
    //return this.renderModal() 
    return (
     <View> 
     <View> 
      {this.props.children} 
     </View> 
     {this.renderModal()} 
     </View> 
    ) 
    } 
} 

回答

3

要侦听推送通知时,应用程序是在前台或后台这两个代表添加到您的AppDelegate.m底部。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
UA_LINFO(@"Application received remote notification %@", userInfo);  
[RCTPushNotificationManager application:application didReceiveRemoteNotification:userInfo]; } 

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
UA_LINFO(@"Application received remote notification -- UIBackgroundFetchResult: %@", userInfo); 
    [RCTPushNotificationManager application:application didReceiveRemoteNotification:userInfo];} 

这些代表将调用RCTPushNotificationManager桥路过,然后可以通过监听PushNotificationIOS组件上的notification事件从您的应用程序处理的通知数据。您可以从您的反应本机应用程序的顶级组件的componentWillMount注册此处理程序。

PushNotificationIOS.addEventListener('notification', this._onNotification);

1

我通过创建存储阉该应用在背景或前景中的应用状态来实现这一点。当您使用远程通知事件处理程序时,它仅在当前处于应用程序中或来自热通知时才会弹出。所以我只是在应用程序处于后台状态并且收到远程通知时才发送用户。所有在componentDidMount中处理我使用了redux来存储全局状态,但是你可以使用另一个状态处理程序。

componentDidMount() { 

    AppState.addEventListener('change', (appState) => { 
    if (currentAppState === 'background') { 
     this.props.dispatch(updateStatus(false)); 
    } else if (currentAppState === 'active') { 
     this.props.dispatch(updateStatus(true)); 
    } 
    }); 

    PushNotificationIOS.addEventListener('notification', (notification) => { 
    if (this.props.status === false) { 
     let data = notification.getData(); 

     this.props.remoteNavigator.resetTo({}); 
    }); 

} 

在这个例子中this.props.status是我的应用程序在后台或前台。哪些更新由REDX行动updateStatus