Detailed explanation of how to use Android Notification
Use TaskStackBuilder to get the PendingIntent to process click to jump to another activity. First, use the general PendingIntent to jump.
mBuilder = new (this).setContent(view) .setSmallIcon().setTicker("New News") .setWhen(()) .setOngoing(false) .setAutoCancel(true); Intent intent = new Intent(this, ); PendingIntent pendingIntent = (this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); (pendingIntent);
I directly use PendingIntent to jump to NotificationShow. In terms of running effect, I first sent a Notification to the notification bar. Then at this time, I exited the program, that is, MainActivity no longer exists. I went back to the home main menu and saw that Notification still exists. Of course, we have not clicked or canceled it yet. Now click Notification and jumped to NotificationShow interface. Then we press the Back key and found that we went back to the main interface. Now most Android applications are in the notification bar if there is a Notification notification, click it and will jump directly to a certain interface of the corresponding application. If you fall back, that is, press the Back key, you will return to the main interface of the application, rather than the main interface of the system. Therefore, the above PendingIntent method cannot achieve the goal. Here we use TaskStackBuilder to do it.
mBuilder = new (this).setContent(view) .setSmallIcon().setTicker("New News") .setWhen(()) .setOngoing(false) .setAutoCancel(true); Intent intent = new Intent(this, ); TaskStackBuilder stackBuilder = (this); (); (intent); PendingIntent pendingIntent = (0, PendingIntent.FLAG_UPDATE_CURRENT); // PendingIntent pendingIntent = (this, 0, // intent, PendingIntent.FLAG_UPDATE_CURRENT); (pendingIntent);
Display an instance of stackBuilder, then addParentStack(); For this method, let's check the official API documentation: Add the activity parent chain as specified by the parentActivityName attribute of the activity (or activity-alias) element in the application's manifest to the task stack builder. This sentence means adding an activity, which is associated with the parentActivityName property in the manifest file of this activity.
Then we will add this property in the manifest file
<activity android:name="" android:parentActivityName=".MainActivity" > </activity>
Let its parentActivity be MainActivity, which means that when you click back on the NotificationShow interface, it will jump to the MainActivity interface, instead of going back to the main menu of the program as above. Run it, and the final effect is indeed like this.
The above practical Android Notification examples are explained in detail. If you have any questions, please leave a message or go to the community of this website to communicate and discuss. There are many articles on Android development on this website. I hope you can search and read it. I hope you can help you. Thank you for your support for this website!