Reputation: 4268
at the moment i set an notification in my android app like this:
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(getString(R.string.app_name));
builder.setContentText(content);
builder.setTicker(content);
builder.setSmallIcon(R.drawable.ic_notification_appicon);
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
builder.setLights(Color.GREEN, 500, 500);
return builder.getNotification();
}
Problem is, that i would like to press on the notification which should open the app. but if i touch on the notification, nothing happens. any ideas?
Upvotes: 3
Views: 2019
Reputation: 25312
You need to add PendingIntent
in builder.setContentIntent(pendingIntent)
to start actvity.
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(getString(R.string.app_name));
builder.setContentText(content);
builder.setTicker(content);
builder.setSmallIcon(R.drawable.ic_notification_appicon);
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
builder.setLights(Color.GREEN, 500, 500);
// This intent is fired when notification is clicked
Intent intent = new Intent(getApplicationContext(), YouMainActvity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Set the intent that will fire when the user taps the notification.
builder.setContentIntent(pendingIntent);
return builder.getNotification();
}
Upvotes: 1
Reputation: 2985
You could find a whole example here: http://android-er.blogspot.bg/2013/06/start-activity-once-notification-clicked.html or try something like this:
private static final int MY_NOTIFICATION_ID=1;
Intent myIntent = new Intent(context, DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Notification myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Notification!")
.setContentText("Do Something...")
.setTicker("Notification!!!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
Upvotes: 0
Reputation: 107141
You need to set setContentIntent to your notification builder
Intent resultIntent = new Intent(this, YourActivity.class);
PendingIntent yourPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(yourPendingIntent);
You can refer this tutorial for more information.
Upvotes: 2
Reputation: 5599
You should set a contentIntent for your notification. Start your learning tour here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Actions or here: http://www.vogella.com/tutorials/AndroidNotifications/article.html#notificationmanager_configure
Upvotes: 1