Откройте конкретную ссылку с помощью push-уведомления Android

#android #notifications

#Android #уведомления

Вопрос:

Следующий код представляет собой код push-уведомления, которое отображается каждый раз, когда происходит новое обновление приложения.

Как можно настроить, чтобы при нажатии на уведомление открывалось окно PlayStore? То есть ссылку.

Следует ли изменить намерение?

 public static class NotificationServiceNuevaActualizacion extends IntentService {

        private NotificationManager notificationManager;
        private PendingIntent pendingIntent;
        private int NOTIFICATION_ID = 4;
        Notification notification;

        public NotificationServiceNuevaActualizacion(String name) {
            super(name);
        }

        public NotificationServiceNuevaActualizacion() {
            super("SERVICE");
        }

        @TargetApi(Build.VERSION_CODES.O)
        @Override
        protected void onHandleIntent(Intent intent2) {
            String NOTIFICATION_CHANNEL_ID = getApplicationContext().getString(R.string.app_name);
            Context context = this.getApplicationContext();
            notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent mIntent = new Intent(this, Principal.class);
            Resources res = this.getResources();
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

            String message = "New update";

            if (SDK_INT >= Build.VERSION_CODES.O) {
                final int NOTIFY_ID = 0; // ID of notification
                String id = NOTIFICATION_CHANNEL_ID; // default_channel_id
                String title = NOTIFICATION_CHANNEL_ID; // Default Channel
                PendingIntent pendingIntent;
                NotificationCompat.Builder builder;
                NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                if (notifManager == null) {
                    notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                }
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = notifManager.getNotificationChannel(id);
                if (mChannel == null) {
                    mChannel = new NotificationChannel(id, title, importance);
                    mChannel.enableVibration(true);
                    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                    notifManager.createNotificationChannel(mChannel);
                }
                builder = new NotificationCompat.Builder(context, id);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentTitle(getString(R.string.app_name)).setCategory(Notification.CATEGORY_SERVICE)
                        .setSmallIcon(R.drawable.logo_utn)   // required
                        .setContentText(message)
                        .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.icono))
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setAutoCancel(true)
                        .setSound(soundUri)

                        .setContentIntent(pendingIntent)
                        .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                Notification notification = builder.build();
                notifManager.notify(NOTIFY_ID, notification);

                startForeground(3, notification);
            }
        }
    }
  

Ответ №1:

Вам нужно будет изменить эту строку кода:

 Intent mIntent = new Intent(this, Principal.class);
  

к этому и замените %APP_ID% на определенный идентификатор приложения:

 Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=%APP_ID%"));
  

Более подробную информацию вы можете найти здесь: https://developer.android.com/distribute/marketing-tools/linking-to-google-play