Android条件性PendingIntent在通知中?

huangapple go评论62阅读模式
英文:

Android Conditional PendingIntent in Notification?

问题

我的应用目前有两个活动:一个闪屏活动,显示应用程序标志并从远程数据库加载用户数据,然后是包含所有片段的主活动。目前,我在通知方面遇到了一些问题。我正在使用 FCM 传递通知,并且已经正常工作,但是对于通知的挂起意图不太清楚该怎么处理。如果应用程序被关闭,我不希望它启动主活动,因为我在闪屏活动中从数据库加载数据。因此,主活动可能会缺少数据(目前由于空指针异常,它会崩溃)。但是,如果应用程序仍在运行,我也不希望再次启动闪屏界面。是否有一种方式可以在通知中添加条件意图呢?

或者,使用通知从活动中启动操作是否是不良实践?我绝不是 Android 专家,所以可能有更好的方法。然而,我更愿意保持当前的活动流程,其中闪屏界面负责初始数据加载。我希望只需在通知点击时广播一条消息,如果任何活动具有活动侦听器,那么它将只消耗这些数据。

英文:

My application currently has two activities: a splash activity which displays the app logo and loads the user’s data from a remote database, and then the main activity that houses all my fragments. I’ve reached a bit of an impasse right now with notifications. I’m using FCM to deliver the notifications and have that working fine, but don’t really know what to do with the pending intents of the notifications. If the app is closed, I don’t want it to launch the main activity since I load the data from the DB in the splash activity. Thus the main activity would have missing data (it currently crashes due to null pointer exceptions everywhere). But if the app is still running, I don’t want the splash screen to be launched again. Is there some type of way to have a conditional intent baked into the notification?

Alternatively, is this just bad practice to do what I’m doing with launching the activities from the notifications? I’m by no means an android expert so there might indeed be a better way. I would prefer to keep my current activity flow however where the splash screen is responsible for the initial loading of data. I was hoping there was some way to just broadcast a message on the click of the notification and if any activities have an active listener then it would just consume that data.

答案1

得分: 0

我想到的解决方案是创建一个单一的活动,其唯一目的是处理通知,然后通过新意图(如果应用程序被终止)或广播(如果应用程序被恢复)将通知传递给适当的活动。不确定这是否是不良做法(如果有人告诉我这是否违反了任何类型的软件原则,我会很感激)。

到目前为止,这个解决方案运行得很好,而且坦率地说,把所有的逻辑放在一个活动中确实非常方便——服务所需做的就是将通知中的数据和动作传递给活动,然后活动会完成剩下的工作。

英文:

The solution I have come up with is to have a single activity whose sole purpose is to process notifications and then pass them to the appropriate activity either through a new intent (if app is terminated) or through a broadcast (if app is resumed). Not sure if this is bad practice (would appreciate anyone telling me if this violates any type of software principle).

It's working like a charm so far and is honestly really nice to have all that logic in one activity anyway - all the service has to do is pass in the data from the notification and the action and the activity does the rest.

huangapple
  • 本文由 发表于 2020年8月30日 11:05:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定