如何在Android上正确发送重复通知
我在Android上设置了一个重复通知,但是闹钟没有出现在我的设备上 我已经阅读了Android开发者的文档,根据它,我的代码看起来很好,但仍然不能正常运行。我使用一个类作为BroadcastReceiver,它从MainActivity接收意图,然后将其传递给IntentService 这是在MainActivity上触发报警的方法,它在每次初始化应用程序时运行如何在Android上正确发送重复通知,android,notifications,Android,Notifications,我在Android上设置了一个重复通知,但是闹钟没有出现在我的设备上 我已经阅读了Android开发者的文档,根据它,我的代码看起来很好,但仍然不能正常运行。我使用一个类作为BroadcastReceiver,它从MainActivity接收意图,然后将其传递给IntentService 这是在MainActivity上触发报警的方法,它在每次初始化应用程序时运行 public void setAlarm(){ alarmManager = (AlarmManager)
public void setAlarm(){
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast( MainActivity.this, 0, alarmIntent, 0);
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 16);
alarmStartTime.set(Calendar.MINUTE, 36);
alarmStartTime.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.i(TAG,"Alarms set every day.");
}
这是使用BroadcastReceiver的类
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "FOCUSALARM";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "BroadcastReceiver has received alarm intent.");
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
这是使用IntentService的类
public class AlarmService extends IntentService
{
private static final int NOTIFICATION_ID = 101;
private static final String TAG = "FOCUSALARM";
public AlarmService() {
super("AlarmService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// don't notify if they've played in last 24 hr
Log.i(TAG,"Alarm Service has started.");
Context context = this.getApplicationContext();
Intent mIntent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "Focus");
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
// .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.notification_title))
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_subject));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Log.i(TAG,"Notifications sent.");
}
当我运行它时,它会显示所有日志,但通知不会出现在设备上
I/FOCUSALARM: Alarms set every day.
I/FOCUSALARM: BroadcastReceiver has received alarm intent.
I/FOCUSALARM: Alarm Service has started.
I/FOCUSALARM: Notifications sent.
正如我们感谢的投稿人@RobCo所说:在Oreo+设备中,您必须创建一个通知通道,为此,请使用Android开发者文档中所示的以下方法:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Hola";
String description = "Focus bro";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
然后在notificationBuilder上使用相同的notificationManager对象
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
// .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
// .setTicker(res.getString(R.string.notification_title))
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_subject));
notificationManager.notify(NOTIFICATION_ID, builder.build());
Log.i(TAG,"Notifications sent.");
您没有为通知设置通道,因此它不会显示在Oreo+设备上。我建议使用workManager而不是alarm manager和serviceYou's right@RobCo,它终于起作用了,我只需将通知通道添加到notificationManager