Vik0809
Vik0809

Reputation: 379

Notification OnClick Event

I want to call the Activity NotificationReceiver, when I click onto an android notification.

The notification is already shown correctly, but when I click onto the notification nothing happens.

Here is the code for the notification:

 private void notification_anzeigen2(){

    Log.d("Test2", "Test2 ");
    Intent intent = new Intent(this, NotificationReceiver.class);
    intent.putExtra("NotiClick",true);


    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, Intent.FILL_IN_ACTION);

    Notification n  = new Notification.Builder(this).setContentTitle("GestureAnyWhere Hintergrundservice")
                                                    .setContentText("Bitte klicken, um den Hintergrundservice zu beenden")
                                                    .setContentIntent(pIntent)
                                                    .setAutoCancel(true)
                                                    .setSmallIcon(R.drawable.ic_launcher)
                                                    .build();


    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(1, n);
    Log.d("Test3", "Test3 ");
}

And here is the activity:

public class NotificationReceiver  extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {


    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null)
        {
            Log.d("Test4", "Test4");
        }
        else if (extras.getBoolean("NotiClick"))
        {
            Log.d("Test5", "Test5");
            stopService(new Intent(getApplicationContext(), HintergrundService.class));
        }

    }



    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
}

Thanks a lot.

Upvotes: 2

Views: 3804

Answers (1)

Pdroid
Pdroid

Reputation: 867

Try:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

You pass as a flag a constant that has nothing to do here, so its value can match to an undesired flag, see PendingIntent doc.

It says: "May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens."

Upvotes: 1

Related Questions