Heshan Perera
Heshan Perera

Reputation: 4630

Android : Unable to launch activity from notification

I have the following code for dispatching a notification...

private void publishNotification(Intent intent, String header, String content){
        try {
            NotificationManager manager = getNotificationManager();
            Notification notification = new Notification(R.drawable.droid, header, System.currentTimeMillis());

            if(intent != null){
                PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                notification.setLatestEventInfo(this, header, content, pendingIntent);
            }

            manager.notify(0, notification);

        } catch (Exception ex) {
            FileManager.writeToLogFile(this.getClass(), "publishNotification", LogMessageType.ERROR, ex.getMessage());
        }
    }

And the following code that calls on it...

Intent intent = new Intent();
                intent.setAction(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                publishNotification(intent, "Default Password Changed", "Device configuration have changed.\nClick here to change this password.");

My problem is as follows...

The notification appears on the notification and even after it is slid down. The problem is that when I click on this notification, the activity does not launch. I think I have done everything mentioned in the tutorial. What am I doing wrong ?

Upvotes: 1

Views: 995

Answers (2)

Niranj Patel
Niranj Patel

Reputation: 33258

Intent intent = new Intent(context, class_name.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

and also use PendingIntent.getActivity() instead of PendingIntent.getService().

Upvotes: 2

user658042
user658042

Reputation:

If you want to launch an activity, you should use PendingIntent.getActivity() instead of PendingIntent.getService().

Upvotes: 4

Related Questions