0

Is there a way to redirect - when a user clicks the notification in notifications ui - to another activity?

For example

I create an event in Google Calendar and when there is a notification, when a user clicks on it, it will open my Activity rather than go directly to Google Calendar.

ashatte
  • 5,442
  • 8
  • 39
  • 50
jantox
  • 2,185
  • 4
  • 21
  • 22

1 Answers1

0

Try this :

private void generateNotification(Context context, String message,
long when, String query) {
int icon = R.drawable.icon;
 long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;

Intent intent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    intent, 0);

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
    notification = new Notification(icon, message, when);
    notification.setLatestEventInfo(context, appname, message,
            contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify((int) when, notification);

} else {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    notification = builder.setContentIntent(contentIntent)
            .setSmallIcon(icon).setTicker(appname).setWhen(when)
            .setAutoCancel(true).setContentTitle(appname)
            .setContentText(message).build();

    notificationManager.notify((int) when, notification);

}

Hope this helps.

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69