2016-07-15 89 views
0

因此,當用戶點擊傳入的推送通知時,我想讓用戶進入特定的導航器場景。所使用的「場景」和「ID」以JSON形式通過通知有效內容。React Native:從點擊推送通知調用函數?

但我不知道如何處理這些值

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

「通知」事件中。

很明顯,當點擊通知時會出現應用程序,但是如何在一次它觸發一個函數?

謝謝!

回答

1

在你最上面的組成部分,一個在你index.ios.js註冊,在componentDidMount方法,你可以從通知中添加監聽器和get the data

export default class App extends Component { 
    componentDidMount() { 
     PushNotificationIOS.addEventListener('notification', this._onNotification.bind(this)); 
     if (notification) { 
      const data = notification.getData(); 
      if (data) { 
       // do your thing 
      } 
     } 
    } 
} 

另一種解決方案是傳遞一個url,並執行下面的代碼,同樣在你最上面的組件的ComponentDidMount方法中:

Linking.getInitialURL().then(url => { 
    if (!url) { 
    return null; 
    } 
    // do something to decide which component to load 
    navigator.push({ 
    component: YourComponent, 
    passProps: { 
     // your props 
    }, 
    }); 
    return null; 
}).catch(() => null); 
+0

perfect- thanks! –

相關問題