android - 如何知道应用程序是否从 android 通知托盘打开?

标签 android react-native react-native-push-notification

如何知道应用程序是否从 android 通知托盘打开?例如,我关闭了应用程序(从最近的应用程序列表中清除)。但我收到来自后端 websocket 的通知,我按下它,它打开了应用程序。所以我的问题是,有没有办法检查这是否已从通知中打开?

最佳答案

查看 react-native-push-notification 的来源+ 接下来的 50 行(直到 setContentIntent),您可以检查 Intent 中额外的“通知”。

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getBundleExtra("notification");
        if(bundle != null){
            //check if it is the bundle of your notification and do your thing
        }
    }

否则,您可以使用原生模块方法:

当您设置传递给通知.setContentIntent() 方法的PendingIntent 时,指定您随后在应用程序中恢复的操作。示例通知:

Intent intent = new Intent(context, MyActivity.class);
intent.setAction("OPEN_MY_APP_FROM_NOTIFICATION");
NotificationCompat.Builder mNotifyBuilder = NotificationCompat.Builder(this, CHANNEL)
            .setContentTitle("Title")
            .setContentIntent(PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT))            
mNotificationManager.notify(Notification_REQUEST_CODE,
                                    mNotifyBuilder.build())

在 MyActivity.java 中

public void onCreate (Bundle savedInstanceState) {
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    if(action == "OPEN_MY_APP_FROM_NOTIFICATION"){
         //do whatever you have to do here
    }
}

附加信息: Handling intents Creating Intents

关于android - 如何知道应用程序是否从 android 通知托盘打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52597193/

相关文章:

java - dataOutputStream.readFully 的进度

javascript - 在 React Native 中安排本地通知不起作用

android - React Native : AppState. addEventListener 在点击通知时在恢复时注册重复事件

android - 在原始/发布构建期间排除原始/或特定文件

android - 无法从服务器连接到 Google API (401 : Unauthorized)

react-native - 使用 componentWillMount 和 componentDidMount 响应 native setState(...) 警告

ios - 在 iPhone 上运行 React Native 应用程序

android - 此过程中未初始化默认 FirebaseApp - React Native - 本地通知

android - Activity 内容转换不起作用

android - React-native android 构建失败 : "...AAPT: error: resource android:attr/fontVariationSettings not found."