Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android call method on notification click

Tags:

This code creates a notification. If you click it, the current application is ran (the intent is created in Entry, which is my only Activity), a slightly modified version of a Android Developers blog:

private void makeIntent() {
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification note = new Notification(R.drawable.prev, "Status message!", System.currentTimeMillis());
    Intent intent = new Intent(this, Entry.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    note.setLatestEventInfo(this, "New Email", "Unread Conversation", pi);
    note.flags |= Notification.FLAG_AUTO_CANCEL;
    mgr.notify(NOTIFY_ME_ID, note);
}

But I don't want to start any activity, but merely to run a method in the current activity. From what I've read so far, I guess I have to use methods like startActivityForResult(), use intent-filters and implement onActivityResult(), but after messing around with all those things, changing things in the Intent and PendingIntent, I still have no usable result. Is it possible to somehow just call a method in Entry (my main Activity, in which the Intent is created), or catch any outgoing or incoming Intents when I click my newly made Notification?

PS. my apologies if this is a duplicate thread, SO is quite slow right now, I can't search properly.

like image 869
stealthjong Avatar asked Dec 11 '12 15:12

stealthjong


2 Answers

Add android:launchMode="singleTop" in your activity in your manifest file, have the method protected void onNewIntent(Intent intent) { ... } and use this code:

private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;

void notification() {   
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(R.drawable.next, "Notification!", System.currentTimeMillis());
    Context context = getApplicationContext();
    String notificationTitle = "Exercise of Notification!";
    String notificationText = "http://android-er.blogspot.com/";
    Intent myIntent = new Intent(this, YourActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION);
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
like image 134
MC Emperor Avatar answered Sep 23 '22 20:09

MC Emperor


This worked 100% for me:

Place this code in a method:

Intent intent = new Intent(this, YourClass.class);
    intent.putExtra("NotiClick",true);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        Notification Noti;
        Noti = new Notification.Builder(this)
                .setContentTitle("YourTitle")
                .setContentText("YourDescription")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, Noti);
    }

Then in the onCreate/constructor of your class do this:

if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) 
        {
            //Cry about not being clicked on
        } 
        else if (extras.getBoolean("NotiClick"))
        {
            //Do your stuff here mate :)
        }

    }
like image 21
Michael Schaller Avatar answered Sep 19 '22 20:09

Michael Schaller