Get the PendingIntent event when notification is c

2019-02-26 04:33发布

I'm trying to get the event click when a notification is clicked.

What I have

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Test";
Notification mNotification = new Notification(R.drawable.notification_template_icon_bg, MyText, System.currentTimeMillis() );
String MyNotificationTitle = "Test!";
String MyNotificationText  = "Test!";
Intent MyIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
makeToast(StartIntent.getIntentSender().toString());
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
notificationManager.notify( NOTIFY_ME_ID, mNotification);

This is working perfect, but the thing that I don't know how to do is get the click on that notification.

What I've tried

I tried to do something on onUserInteraction() that if I'm not wrong seems to get fired when Intent starts a new activity, but didn't work.

Also I've tried on onActivityResult() but I don't know how to get that current Intent.

And the last thing that I've tried is doing something like this

BroadcastReceiver call_method = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
     String action_name = intent.getAction();
            if (action_name.equals("MyIntent")) {
                //STUFF HERE
            }
   };
};
registerReceiver(call_method, new IntentFilter("MyIntent"));

Also instead of put MyIntent that is the Intent, I've tried to put the PendingIntent but doesn't work.

By the way on my code appears this when I try to create a Notification

enter image description here

And this when I try to call the setLatestEventInfo()

enter image description here

But I don't know if it may be the cause of the problem or if it could bring problems in the future.

What I'm doing wrong?

EDIT

I've just created a sample what my app does at the moment. It's simple when I press a Button it pop ups a Notification. On my real APP I don't have to click a Button but it's the same. The thing that I want is get the event of the click on the Notification and make things with that event. The thing that I've done is create another Activity where I put the things that I want and then on onCreate() at the end of the things that I want to do I call Finish() method to finish that Activity, but I don't know if it's the best approach. I want another way to do it I don't want to use two Activities...

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnoti;
private static final int NOTIFY_ME_ID=1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnoti = (Button)findViewById(R.id.btnoti);
    btnoti.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.btnoti){
        addNotification();
    }
}

private void addNotification() {

    //We get a reference to the NotificationManager
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String MyText = "Test";
    Notification mNotification = new Notification(R.mipmap.ic_launcher, MyText, System.currentTimeMillis() );
    String MyNotificationTitle = "Test!";
    String MyNotificationText  = "Test!";
    Intent MyIntent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);

    PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
    notificationManager.notify( NOTIFY_ME_ID, mNotification);
}

Edit 2 (Three fast questions) to go ahead with my code...

I hope you don't mind to solve to me that three fast questions...

  1. Since now I've used Thread.sleep() to do a task for example every 30 seconds with a while(true) but I don't know if it's the best way because I want to let the user choose the time, for example time could be 5 min or 5h... And I don't know what's the best way to take, I've read that theres a method or something called AlarmManager is the correct way to repeat tasks? Is there any source sample to know how to use this Alarm Manager?
  2. I've to know when the user make a "finish()" from the Intent (ACTION_PICK_WIFI_NETWORK) I mean when I'm back to my APP after close that Intent I've have used onResume() but I don't know if it's the correct way to work with, isn't it? (If you don't understand what I'm traying to say it's simple, I want to know the name of the event that know when the user closes the Wifi network picker)
  3. Is this a way to make your APP that still alive once you go to another APP? I mean you can go to another APP and your APP is still working without user interact? Because since now, If I go to another APP my app is like sleep or something and doesn't keep running.... I've read something to call the tasks with a Service and I think it goes well, and it still running even if the APP isn't in the Recent APP...

Thanks, if you can't answer me I can make a post for each question but I think those question could be fast to answer.

1条回答
萌系小妹纸
2楼-- · 2019-02-26 05:10

Normally when we start an Activity from a push notification, the Intent is recovered in the started Activity's onCreate() method.

In your case, you are starting a system-defined Activity using the WifiManager.ACTION_PICK_WIFI_NETWORK action. We do not have access to the onCreate() of this Activity, so it doesn't seem possible to recover the Intent from it.

Because you have created a PendingIntent using getActivity(), its not possible to intercept the Intent using a BroadcastReceiver. A BroadcastReceiver is triggered by a PendingIntent created using getBroadcast().

Also, it is most certainly wrong to use a PendingIntent in place of an Intent (or vice-versa). These two are not really objects of the same type. As such they are not interchangeable. If you observe their namespace classification, it is android.app.PendingIntent versus android.content.Intent.

I know that this is not a "real" answer to your question, but its all I have for now. I will update this answer if I can think of a solution ... Best.

查看更多
登录 后发表回答