Почему открытие действия нажатием кнопки уведомления не работает в Android?

#android #push-notification #pushwoosh

#Android #push-уведомление #pushwoosh

Вопрос:

Я интегрирован PushWoosh в свой проект. И мне нужно открыть действие, когда пользователь нажмет на уведомление. Когда push был получен, я должен получить данные (скажем, идентификатор) и отправить этот идентификатор с помощью Intent и открыть свою активность. Итак, я создал фабрику (для пользовательских push-уведомлений) и в GenerateNotification() обратном вызове создал уведомление. Но когда я устанавливаю ожидающее намерение и после этого нажимаю на уведомление, оно открывает мое основное действие.

 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) {
        }
    }
  

Но перенаправление работает, если я помещаю

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

Ответ №1:

Наконец, я нашел решение. Для того, чтобы открыть пользовательское действие, нажав на уведомление, нам нужно создать BroadcastRecevier и начать действие с этого

 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);

    }
}
  

И не забудьте добавить его в манифест

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

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

Ответ №2:

Удалить PendingIntent pendingIntent = ... , затем добавить : setNotifyIntent(pushIntent)

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