android - 如何通过推送通知检测应用程序是否打开?
问题描述
我对如何检测我的应用程序是否从 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();
}
}
它工作正常,但我不确定这是一个好习惯。有没有人有一些想法?
解决方案
推荐阅读
- javascript - 即使组件已卸载,状态也保持不变
- sql - IS NULL 和 IS NOT DISTINCT FROM NULL 之间有区别吗?
- php - SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens - help needed
- c++ - Crypto++ GetModulus() 问题
- ios - 网络请求后 SwiftUI 更新 Elements.count
- javascript - 在 Promise 中,response.status 返回其他 Promise 而不是状态码
- azure - AD B2C:内置登录注册流程的让我登录 cookie 过期时间
- reactjs - Auth0 React SPA API 的访问令牌不正确
- angular - 使用 Proxy.Conf.js 的 Angular 8 显示用户名和密码框
- powershell - 在powershell中将元素添加到数组