Click the notification in the notification bar in Android to start the activity. Problem solving

preface

Recently, I met a very wonderful problem and finally solved it. So I want to record it so that everyone or myself can refer to it when they need it in the future.

Problem scenario

Use Xiaomi mobile phone to push a message with Xiaomi, then click the message in the notification bar to start the application, and then enter the activity of the session. After the application is started, if the current interface is not a session interface, a message reminder will be displayed in the notification bar for the new message, and then click the session message, but the activity of the session cannot be entered. That is, after clicking the notification bar, the system does not mean to start the specified activity, does not see the log of the system starting the activity, and will see the shadow of the system processing the activity.

The specified activity is not the activity of the session, but the activity a of android.intent.category.launcher is specified in the androidmanifest.xml file. In other words, all session messages start from this a, and then transfer the data to the subsequent activity.

There are two ways to display notifications. One is to pop up in the notification bar by the mobile phone system. For example, Xiaomi push is used on Xiaomi mobile phones and Huawei push is used on Huawei mobile phones. The other is to pop up by the remote process of the application.

There are also two ways to start the first activity a of the application. One is to construct an intent directly through new, and then pass in the class of activity a; The other is to obtain the content of the started activity a through context. Getpackagemanager(). Getlaunchintintforpackage (context. Getpackagename()). Then we call the PendingIntent.getActivity () method to import the resulting intent.

Then the problem comes. If you click the notification bar that pops up in the system or the notification bar that pops up in the remote process, if you only use one of the startup methods to start the application, after the application is started, click the new message notification that pops up in the notification bar by the background remote process, and you can't enter the activity of the session at this time. From the system log, the activity was not started, but was processed.

Some people may wonder whether to add an intent.flag_ ACTIVITY_ NEW_ Task ID, because this ID is added in the getlaunchintintforpackage() method.

Finally, the test found that as long as the application is not started, whether clicking the notification bar popped up by the system or the notification bar popped up by the remote process, if you receive a new message notification, and then click the notification bar, you can enter the session activity. Then just judge whether an activity in the application is started. It seems that the problem can be solved.

Problem solving

Therefore, the following logic is used to judge whether there is a foreground activity running.

expand

However, the above method works well on Xiaomi mobile phones, but there are still problems on Huawei mobile phones, even in the same scenario. Huawei is cheating again!

So we started with the status of the importance variable in the activitymanager.runningappprocessinfo class above, and then tested the variable values that may appear in various scenarios. The results showed that the effect was not satisfactory, and the problems in some scenarios remained.

Finally, another way of thinking: instead of starting the application from activity a, change to activity B, that is, pass in the intent object by calling the pendingintent. Getactivity() method, and use the class of B. If you start B and find that the application has not been initialized, jump to a to perform initialization, and then go through the normal process.

Then test for various scenarios and models to find and solve the problems. It can be seen from the above that although you don't understand the principle behind it, you must have a wide range of ideas to solve the problem, especially when you are in a hurry to send a version, don't hang from a tree.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>