Я довольно новичок в разработке Android. Мне удалось получить всплывающее уведомление, когда приложение находится в фоновом режиме. Когда я нажимаю на него, он успешно загружает резервную копию приложения. Однако я хочу загрузить предупреждение со страницы, но только тогда, когда оно открывается из уведомления.
Вот код для генерации уведомления. Любая помощь будет оценена по достоинству.
private void getNotificationForPasswordChange() {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Hello";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent mainIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Pronto Tracker")
.setTicker("Pronto Tracker")
.setContentText("Cannot connect to server. Location is not being updated.")
.setSmallIcon(R.mipmap.ic_pronto_logo)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setOngoing(true).setContentIntent(mainIntent).
build();
mNotificationManager.notify(Constants.PASSWORD_CHANGE_NOTIFICATION_ID, notification);
}
Вы должны использовать функцию onCreate в своей MainActivity Добавьте этот код, чтобы проанализировать ваше намерение: Получено намерение Intent = getIntent();
Вы можете передать предупреждающее сообщение с уведомлением PendingIntent. Добавьте сообщение или значение, которое вы хотите отобразить как предупреждение в PendingIntent .putExtra(), а также укажите действие в PendingIntent, где вы хотите отобразить предупреждение в виде диалога или чего-либо еще.
Intent intent = new Intent(Application.getAppContext(), MainActivity.class);
intent.putExtra("is_notification", true);
intent.putExtra("alert_message", "Hello World!");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
После этого добавьте PendingIntent в свое уведомление. Второе, что вам нужно сделать, это получить данные из Intent, когда пользователь нажимает на уведомление. В MainActivity добавьте следующий код для получения данных из Intent: -
if (getIntent() != null) {
String message = getIntent().getStringExtra("alert_message");
boolean isNotification = getIntent().getBooleanExtra("is_notification", false);
if (is_notification){
// show alert
}
}