The intent in notification does not work
android, android-intent, android-notifications, android-pendingintent
Solution
If you intend to start the Activity when Notification is clicked: Use:
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
in place of:
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().
Problem
My notification is a service (service.java) and what the service do is start a notification of battery level when a checkboxpreference in the preference screen is checked.. What not works now is the intent to enter in the `MainActivity` clicking the notification. This is the code ``` if(mprefs.getBoolean("notification_a", false)==true){ notificationBuilder = new NotificationCompat.Builder(context); notificationBuilder.setOngoing(true); notificationBuilder.setContentTitle("Battery Stats Informations"); notificationBuilder.setContentText("Carica residua: " +level+"%" + " " + "Temperatura: " +temperature+ "°C"); //notificationBuilder.setTicker("Informazioni batteria"); notificationBuilder.setWhen(System.currentTimeMillis()); notificationBuilder.setSmallIcon(R.drawable.icon_small_not); Intent notificationIntent = new Intent(context, MainActivity.class); PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0); notificationBuilder.setContentIntent(contentIntent); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); Notification not=notificationBuilder.build(); mNotificationManager.notify(SIMPLE_NOTIFICATION_ID,not); } else { mNotificationManager.cancelAll(); } ``` The intent `Intent notificationIntent = new Intent(context, MainActivity.class` doesn't work. Any helps?