Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start activity when user clicks a notification?

I am attempting to convert some code I found in a tutorial for my own use. Originally, the code launched the system contacts list when the user would click a notification generated by my app. I am trying to start an Activity of my own instead of launching the contact list, but it's not working. More specifically, nothing happens. There is no error, and my Activity doesn't load either. The notification window disappears after clicking, and the original Activity is still visible.

Here is my code:

public class MyBroadcastReceiver extends BroadcastReceiver {
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;

    public void onReceive(Context context, Intent intent){
        Bundle extras = intent.getExtras();

        String deal = (String) extras.get("Deal");
        String title = "Deal found at " + (String) extras.get("LocationName");

        mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notifyDetails = new Notification(R.drawable.icon, title,System.currentTimeMillis());

        Class ourClass;
        try {
            ourClass = Class.forName("com.kjdv.gpsVegas.ViewTarget");
            Intent startMyActivity = new Intent(context, ourClass);

            PendingIntent myIntent = PendingIntent.getActivity(context, 0,startMyActivity, 0);
            notifyDetails.setLatestEventInfo(context, title, deal, myIntent);
            notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
            notifyDetails.flags |= Notification.DEFAULT_SOUND;
            mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

This is my entry in the AndroidManifext.xml file...

  <activity android:name=".ViewTarget" android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.kjdv.gpsVegas.ViewTarget" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
   </activity>

And this is my Activity that I want to launch...

public class ViewTarget extends ListActivity {
    public ListAdapter getListAdapter() {
        return super.getListAdapter();
    }

    public ListView getListView() {
        return super.getListView();
    }

    public void setListAdapter(ListAdapter adapter) {
        super.setListAdapter(adapter);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locations);
        Log.v("db", "Inside ViewTarget");
    }
}
like image 853
Kevin Avatar asked Apr 17 '12 02:04

Kevin


People also ask

How do I get apps to open automatically when I get push notifications?

here's what you can try out: Create an intent with action=MAIN and category=LAUNCHER. Get the PackageManager from the current context using context. getPackageManager.


1 Answers

Which Android version are you running on? You might wanna try using NotificationCompat instead. This class is include in the latest support package.

Intent notificationIntent = new Intent(context, ViewTarget.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
                0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
       .setSmallIcon(R.drawable.app_icon)
       .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.app_icon))
       .setTicker(payload)
       .setWhen(System.currentTimeMillis())
       .setAutoCancel(true)
       .setContentTitle("Message")
       .setContentText(payload);
Notification n = builder.getNotification();

n.defaults |= Notification.DEFAULT_ALL;
nm.notify(0, n);

EDIT: I know this is an old thread/question but this answer helped me for showing the activity when tapping the notification. For those people that this isn't working is probably because you haven't "registered" the activity in your manifest. For example:

<activity
   android:name="com.package.name.NameOfActivityToLaunch"
   android:label="Title of Activity" >
   <intent-filter>
     <action android:name="com.package.name.NAMEOFACTIVITYTOLAUNCH" />

     <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</activity>

And, hopefully, this should work. Hope it helped...

like image 196
RobGThai Avatar answered Sep 29 '22 11:09

RobGThai