Mikhail Valuyskiy
Mikhail Valuyskiy

Reputation: 1298

Why opening activity by pushwoosh notification click doesn't work in android?

I integrated PushWoosh in my project. And I need to open the activity when the user tap on the notification. When push was received I should obtain the data (let's say id) and send this id using Intent and open my Activity. So, I have created Factory (for custom push notifications) and in GenerateNotification() callback, created notification. But when I set Pending intent and after this clicking on the notification it opens my main activity.

public class MyFactory extends AbsNotificationFactory {
        private String id;

        @Override
        public Notification onGenerateNotification(PushData pushData) {
            final String notificationTitle = "Title";

            id = pushData.getExtras().getString("Id");

            final Intent pushIntent = new Intent(getContext().getApplicationContext(), PushActivity.class);
            pushIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            pushIntent.putExtra("Id", id);

            final int uniqueId = Math.abs(UUID.randomUUID().hashCode());

            PendingIntent pendingIntent = PendingIntent.getActivity
                    (getContext(), uniqueId, pushIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.setBigContentTitle(notificationTitle);
            bigTextStyle.bigText(notificationAlert);

            final NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
                .setSmallIcon(R.drawable.app_icon)
                .setContentTitle(notificationTitle)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent)
                .setContentText(notificationAlert)
                .setStyle(bigTextStyle);

            final Notification notification = builder.build();
            final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
            //notificationManager.notify(uniqueId, notification);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            return notification;
        }

        @Override
        public void onPushReceived(PushData pushData) {
        }

        @Override
        public void onPushHandle(Activity activity) {
        }
    }

But redirection works if I put

  notificationManager.notify(uniqueId, notification);
  return null;

Upvotes: 1

Views: 607

Answers (2)

Pouicky
Pouicky

Reputation: 149

Remove PendingIntent pendingIntent = ... then add : setNotifyIntent(pushIntent)

final Intent pushIntent = new Intent(getContext().getApplicationContext(), PushActivity.class);
...
setNotifyIntent(pushIntent);

Upvotes: 0

Mikhail Valuyskiy
Mikhail Valuyskiy

Reputation: 1298

Finaly, I found the solution. In order to open custom activity via tapping on Notification we need to create BroadcastRecevier and start activity from this

public class PushNotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent incomingIntent) {
        if (incomingIntent == null)
            return;

        // Get data here if need 
        // From incomingIntent and PushBundle

        Intent intent = new Intent(context,PushReceiverActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

        //Let Pushwoosh SDK post-handle push (track stats, etc.)
        PushManagerImpl.postHandlePush(context, incomingIntent);

    }
}

And do not forget to add it to the manifest

 <receiver android:name=".PushNotificationReceiver" />

        <meta-data
            android:name="PW_NOTIFICATION_RECEIVER"
            android:value="com.example.test.pushwooshtest.PushNotificationReceiver" />

Upvotes: 1

Related Questions