How do I know that the notification displayed in the notification bar has been clicked?

Source: Internet
Author: User

How do I know that the notification displayed in the notification bar has been clicked?


It seems that no one has such a demand? Let me talk about my needs!


The company develops push ads, and then I need to know if the user has clicked the notification to count the click rate!

Of course, the general push is to start your own activity, so you can listen when your activity starts!


But I designed it like this:

1. If the application has been installed, intent opens the application.

2. If a webpage is pushed, intent calls the system browser to browse the webpage.

3. If the push application is not installed, intent starts the installation.

This is not a target activity at all. What should we do?


First, let's look at how to display notifications and check the Code:

Intent intent = new intent (mainactivity. this, testreceiver. class); intent intent2 = new intent (intent. action_view, Uri. parse ("http://wap.baidu.com"); intent2.addflags (intent. flag_activity_new_task); intent. putextra ("intent", intent2); Notification = new notification (R. drawable. ic_launcher, null, system. currenttimemillis (); pendingintent contentintent = pendingintent. getbroadcast (this, 0, Intent, pendingintent. flag_update_current); notification. setlatesteventinfo (this, "good morning! "," Today is a clear day! ", Contentintent); notification. Flags | = notification. flag_auto_cancel; icationicationmanager mnm = (icationicationmanager) getsystemservice (context. icationication_service); mnm. Notify (1001, notification );

Notifycation requires a pendingintent

1. getactivity gets the pendingintent that starts an activity.

2. getservice starts the service

3. getbroadcast starts broadcastreceiver


Take getbroadcast as an example. The third parameter is intent, which is the intent of the broadcastreceiver to be started. However, intent itself can use putexter () to store an intent object.

: You only need to put the true intent into the intent that starts broadcastreceiver, and then extract the intent from the onreceiver of boradcastreceiver to start the real activity, of course, if what you really want to start is not an activity, you can put a flag to determine which components to start!


View code:

public void onReceive(Context context, Intent intent) {System.out.println("onReceive");Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();Intent intent2 = (Intent)intent.getParcelableExtra("intent");//intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent2);}

Note:

1. An error is reported when the activity is directly started in the oncycler context. You need to add the intent. flag_activity_new_task flag to the intent!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.