首页 > 解决方案 > 如何通过推送通知检测应用程序是否打开?

问题描述

我对如何检测我的应用程序是否从 Android 系统创建的推送通知中打开很感兴趣。

因为一些推送通知是由我们的 FirebaseMessagingService 实现创建的,其他的则由系统自动创建。如果在第一种情况下从推送通知打开应用程序时 - 一切都很清楚,我们可以通过额外的意图检测到它,我们放入 FirebaseMessagingService,在第二种情况下我遇到了一些麻烦,因为这个意图是创建的由系统。

我已经尝试了这篇文章中的第二种方法https://www.quora.com/How-is-it-possible-to-track-whether-an-application-was-opened-by-clicking-on-a- Push-Notification-Android#:~:text=Android但失败了。

尽管如此,我还是注意到在意图中额外的“来自”没有设置值“通知”,但是有我的gcm_defaultSenderId 我通过这种方式跟踪它:

Bundle intentExtras = getIntent().getExtras();
if (intentExtras != null && !intentExtras.isEmpty()) {
    String intentSource = intentExtras.getString("from", "none");
    if (intentSource.equalsIgnoreCase(getString(R.string.gcm_defaultSenderId))) {
        trackAppOpenedFromPush();
    }
}

它工作正常,但我不确定这是一个好习惯。有没有人有一些想法?

标签: androidfirebasepush-notificationfirebase-cloud-messagingandroid-notifications

解决方案


推荐阅读