Reputation: 3
My application is meant to send a notification after a certain interval, but the onReceive is not being triggered in my BroadcastReceiver when I use AlarmManager. I have stripped the code back to find the problem but am not having any luck. The setExact doesn't seem to be key as I have tried just set and setExactAndAllowWhileIdle.
NotificationService
public void notification(Context context){
NotificationChannel channel = new NotificationChannel("CHANNEL_1", "CHANNEL_NAME", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("CHANNEL_DESC");
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
nm.createNotificationChannel(channel);
AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if(!alarmManager.canScheduleExactAlarms()){
return;
}
}
Intent alarmIntent = new Intent();
alarmIntent.putExtra("titleExtra", "Title");
alarmIntent.putExtra("textExtra", "Text");
alarmIntent.setAction("TEST_ACTION");
//context.sendBroadcast(alarmIntent);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Toast.makeText(context, "Scheduled for 5 seconds", Toast.LENGTH_SHORT).show();
}
MyBroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("ON RECEIVE");
String message = intent.getStringExtra("textExtra");
String title = intent.getStringExtra("titleExtra");
Notification notification;
notification = new NotificationCompat.Builder(context, "CHANNEL_1")
.setAutoCancel(true)
.setSmallIcon(R.drawable.icon)
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentTitle(title)
.setContentText(message)
.build();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
return;
}
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1234, notification);
}
AndroidManifest.xml
<receiver
android:name=".services.NotificationService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="TEST_ACTION" />
</intent-filter>
</receiver>
When the commented out context.sendBroadcast
is uncommented, the onReceive is hit and the notification is sent, so I don't think the issue is with the onReceive, nor with the notification part of the situation as it doesn't get to the onReceive at all when not working.
However if I add (context, MainMenuActivity.class) to the Intent creations
Intent alarmIntent = new Intent(context, MainMenuActivity.class);
then the sendBroadcast also does not work. I don't know if that is related or a separate issue though.
I also tried creating and registering the receiver programmatically but it behaved the same.
Also have tried with the enabled=true not included and with exported=false and same behaviour.
Happy to add more code or context
Upvotes: 0
Views: 72
Reputation: 96
Try alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ 5000, pendingIntent);
Upvotes: 0