Я пытаюсь отклонить уведомление от .addAction()
, не открывая приложение. Проблема в том, что при нажатии кнопки ничего не происходит, метод onReceive()
не запускается.BroadcastReceiver не получает при нажатии .addAction()
Вот код на MainActivity:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", SOMENUMBER);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(t);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(SOMENUMBER, notification.build());
А на другом классе у меня есть приемник:
public class Notification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
И в приемник на файл AndroidManifest.xml:
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="io.github.seik.Notification" />
</intent-filter>
</receiver>
ли '' MainActivity' продлить Activity' или 'BroadcastReceiver '? Ваше соглашение об именах сбивает с толку :-( –
@DavidWasser Нет, только класс уведомления расширяет BroadcastReceiver. –