Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The intent in notification does not work

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?

like image 876
David_D Avatar asked Aug 20 '13 09:08

David_D


People also ask

What is pending intent in notification?

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.

Which intent is used for sending notification in Android?

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.

What is a pending intent?

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.


1 Answers

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().

like image 134
Nargis Avatar answered Oct 13 '22 01:10

Nargis