0

I have question about notifications. I want to build an app with one buttons called (add notification) when I press add notification a notification is created Then, if I press the notification it takes me back to the activity.

I managed to create a notification but when i press on it, it makes nothing. What do I have to add so the notification sends me back to the activity?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

NotificationManager nmanger;
static int id=1;

public void buclick(View view) {
    NotificationCompat.Builder nbuild= (NotificationCompat.Builder) new  NotificationCompat.Builder(this)
        .setContentTitle("Danger")
        .setContentText("the raining is comming soon")
        .setSmallIcon(R.drawable.amule);

   nmanger =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
   nmanger.notify(id,nbuild.build());
   id++;
}
FlyingPumba
  • 1,015
  • 2
  • 15
  • 37
Dreamer
  • 11
  • 3
  • 1
    From the Android Developers website: https://developer.android.com/training/notify-user/build-notification.html#action I think you are looking for pending intents – Mark Aug 21 '16 at 00:35
  • in my app i have on activity – Dreamer Aug 21 '16 at 00:51

2 Answers2

0

Create PendingIntent, then set it to builder:

Intent intent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder nbuild = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
        .setContentTitle("Danger")
        .setContentText("the raining is comming soon")
        .setContentIntent(pendingIntent)
        .setSmallIcon(R.drawable.amule);
nshmura
  • 5,940
  • 3
  • 27
  • 46
-1

Try this code

Intent intent = new Intent(this, NameOfClassYouWantToOpen.class);

// Create pending intent and wrap our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
try {
    // Perform the operation associated with our pendingIntent
    pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
    e.printStackTrace();
}

http://codetheory.in/android-pending-intents/

What is an Android PendingIntent?

Community
  • 1
  • 1
Kunal Yadav
  • 139
  • 10