检测是否通过推送通知打开了React Native iOS应用程序 [英] Detect whether React Native iOS app was opened via push notification

查看:672
本文介绍了检测是否通过推送通知打开了React Native iOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检测应用是否已启动/通过推送通知打开描述了如何通过用户点击推送通知来检测本机iOS应用程序是否已打开(即启动或仅启动)。

Detect if the app was launched/opened from a push notification describes how to detect whether a native iOS app was opened (that is, either launched or merely made active) via the user tapping a push notification.

如何在React Native中做同样的事情? PushNotificationIOS 让我附加通知监听器......

How can I do the same thing in React Native? PushNotificationIOS lets me attach a notification listener...

PushNotificationIOS.addEventListener('notification', function (notification) {
    console.log('got a notification', notification);
});

但是当前台的应用程序收到推送通知,当我通过推送通知打开应用程序时。

but this fires both when a push notification is received with the application in the foreground, and when I open the app via a push notification.

如何特别检测第二种情况?

How can I detect the second case in particular?

推荐答案

这里有两种情况需要以不同方式检测:

There are two cases here that need to be detected in different ways:


  1. 该应用程序已完全终止(例如,通过重新启动手机,或通过双击主页并将其从后台运行的应用程序列表中轻扫)并通过用户轻按推送启动通知。这可以通过 <$ c来检测(并获取通知的数据) $ c> React.PushNotificationIOS.getInitialNotification 方法。

  2. 该应用已被暂停,并且用户点击推送后再次激活该应用通知。只需在本机应用程序中,您就可以知道这种情况正在发生,因为iOS将点按的通知传递给您的应用打开(即使它是旧通知)并导致您的通知处理程序在您的应用程序处于 UIApplicationStateInactive 状态(或'背景'州,作为React Native的 AppStateIOS 类调用它。)

  1. The app has been completely terminated (e.g. by restarting the phone, or by double-tapping home and swiping it off the list of apps running in the background) and is being launched by a user's tap on a push notification. This can be detected (and the notification's data acquired) via the React.PushNotificationIOS.getInitialNotification method.
  2. The app had been suspended and is being made active again by a user's tap on a push notification. Just like in a native app, you can tell that this is happening because iOS passes the tapped notification to your app when it is opening (even if it's an old notification) and causes your notification handler to fire while your app is in UIApplicationStateInactive state (or 'background' state, as React Native's AppStateIOS class calls it).

处理这两种情况的代码(你可以把它放在你的 index.ios.js 或其他在app启动时运行的地方):

Code to handle both cases (you can put this in your index.ios.js or somewhere else that's run on app launch):

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;
  }
});

这篇关于检测是否通过推送通知打开了React Native iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆