Как установить прослушиватель уведомлений для уведомления?
Я использую следующий код для запуска уведомления при запуске службы через AlarmManager:
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
"Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);
Как установить намерение для этого элемента, чтобы при нажатии на него пользователь запускал определенную активность?
Ответы
Ответ 1
Вам в основном нужно включить класс Activity как часть вашего намерения в ваш PendingIntent. В настоящее время ваш намерение пуст. Чтобы перенаправить на новую активность, это должно быть:
// This line of yours should contain the activity that you want to launch.
// You are currently just passing empty new Intent()
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);
Ответ 2
Что касается комментария yoshi24, вы можете установить такие дополнительные функции.
final Intent intent = new Intent(this, MyActivity.class);
intent.setData(data);
intent.putExtra("key", "value");
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Вы также должны знать об этом, прежде чем идти в ожидании намерений
/questions/13575/how-to-send-parameters-from-a-notification-click-to-an-activity
UPDATE
что-то вроде этого будет работать для вас
int your mainfest
<activity android:name=".MyActivity" android:launchMode="singleTop" ... />
в вашей деятельности
@Override
protected void onCreate(Bundle savedInstanceState) {
processIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
processIntent(intent);
};
private void processIntent(Intent intent){
//get your extras
}
Ответ 3
Я сделал это,
-
Я добавляю Intent.FLAG_ACTIVITY_CLEAR_TOP
к новому намерению
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A new notification", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, NoficationDemoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putString("buzz", "buzz");
intent.putExtras(bundle);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
notificationManager.notify(0, notification);
-
Взаимодействую, я делаю следующее:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(getIntent().getExtras()!=null){
Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
}