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?
In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.
The NotificationManager. notify() method is used to display the notification. The Intent class is used to call another activity (NotificationView. java) on taping the notification.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With