9

Got a little problem that's been bugging me..

I've set up my application to receive PUSH notifications from Urban Airship and that all works fine, but when I tap on a notification in the notifications center, nothing happens.

I want my app to open when a user taps a PUSH notification - what can I do to achieve this?

Any help is as always greatly appreciated.

Thanks

valerybodak
  • 4,195
  • 2
  • 42
  • 53
PinkFloydRocks
  • 808
  • 3
  • 14
  • 29

4 Answers4

10

Create a pending Intent to start the activity and set it in notification using setLatestEventInfo.

Example:

  Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

More info can be found here

San
  • 5,567
  • 2
  • 26
  • 28
  • Where does this code go? I have the same question as the initial poster but I'm not finding the logical place for this. If it matters, I'm using Pushwoosh to send my notifications. – Magua Jan 30 '13 at 04:47
  • This code is for creating the notification. Do you have any receiver class...? Place it in onreceive. – San Jan 30 '13 at 15:34
1

You need to use a custom notification builder and use one of your activities as the PendingIntent.

https://docs.urbanairship.com/android-lib/reference/com/urbanairship/push/CustomPushNotificationBuilder.html

smith324
  • 13,020
  • 9
  • 37
  • 58
1

Following one of their sample projects (https://github.com/urbanairship/android-samples/tree/master/app/src/main/java/com/urbanairship/sample), you can extend the AirshipReceiver class and then override the onReceive method. This did the trick for me:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    String action = intent.getAction();
    if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
        Intent launch = new Intent(Intent.ACTION_MAIN);
        launch.setClass(UAirship.shared().getApplicationContext(), MyHome.class);
        launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launch.putExtra("doWhatever",true);
        UAirship.shared().getApplicationContext().startActivity(launch);
    }

}
Alberto M
  • 1,608
  • 1
  • 18
  • 42
0

you have to call pendingIntent before call .setAutoCancel(true);

look at my builder :

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id).setSmallIcon(R.drawable.logo).setSound(uri).setVibrate(new long[]{1000,1000,1000,1000,1000})
            .setOnlyAlertOnce(true).setContentIntent(pendingIntent).setAutoCancel(true);