简体   繁体   中英

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 ?

如果要启动活动,则应使用PendingIntent.getActivity()而不是PendingIntent.getService()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM