Anonymous
Диспетчер аварийной сигнализации не запускает
Сообщение
Anonymous » 11 мар 2025, 22:20
Я новичок в этом, поэтому я немного потерян.
Код: Выделить всё
public class NotificationController{
Context context;
public void createNotification(Context context){
this.context = context;
Notification notification = getNotification();
//creates notification
Intent intent = new Intent(context, NotificationReceiver.class);
intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
intent.putExtra(NotificationReceiver.NOTIFICATION, notification);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//schedules notification to be fired.
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent);
}
private Notification getNotification(){
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("Reminder")
.setContentText("Car service due in 2 weeks")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent);
return builder.build();
}
}
< /code>
Мой приемник < /p>
public class NotificationReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notification);
}
}
Я также зарегистрировался в в AndroidManifest.
Подробнее здесь:
https://stackoverflow.com/questions/271 ... triggering
1741720846
Anonymous
Я новичок в этом, поэтому я немного потерян.[code]public class NotificationController{ Context context; public void createNotification(Context context){ this.context = context; Notification notification = getNotification(); //creates notification Intent intent = new Intent(context, NotificationReceiver.class); intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1); intent.putExtra(NotificationReceiver.NOTIFICATION, notification); PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0); //schedules notification to be fired. AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent); } private Notification getNotification(){ Intent intent = new Intent(context, MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0); Notification.Builder builder = new Notification.Builder(context) .setContentTitle("Reminder") .setContentText("Car service due in 2 weeks") .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(pIntent); return builder.build(); } } < /code> Мой приемник < /p> public class NotificationReceiver extends BroadcastReceiver { public static String NOTIFICATION_ID = "notification-id"; public static String NOTIFICATION = "notification"; public void onReceive(Context context, Intent intent) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = intent.getParcelableExtra(NOTIFICATION); int id = intent.getIntExtra(NOTIFICATION_ID, 0); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(id, notification); } } [/code] Я также зарегистрировался в в AndroidManifest. Подробнее здесь: [url]https://stackoverflow.com/questions/27103291/alarm-manager-not-triggering[/url]