[英]How to send repeating notifications properly on Android
I'm setting a repeat notification on Android but the Alarm doesnt't appear on my device. 我在Android上设置了重复通知,但“闹钟”未出现在我的设备上。
I have read the Android Developers documentation and according to it my code seems just fine, but still it doesn't run properly. 我已经阅读了Android Developers文档,根据它,我的代码看起来还不错,但仍然无法正常运行。 I am using a class as the BroadcastReceiver that receives the intent from the MainActivity and then it passes it to an IntentService .
我正在使用一个类作为BroadcastReceiver ,它从MainActivity接收意图,然后将其传递给IntentService 。
This is the method that triggers the Alarm on the MainActivity, it runs every time the app is initialized. 这是在MainActivity上触发警报的方法,它在每次应用程序初始化时运行。
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.");
}
This is the class that uses the BroadcastReceiver 这是使用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);
}
}
And this is the class that uses the IntentService 这是使用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.");
}
When I run it it shows all the logs, but the notification is not appearing on the device. 当我运行它时,它会显示所有日志,但通知不会显示在设备上。
I/FOCUSALARM: Alarms set every day.
I/FOCUSALARM: BroadcastReceiver has received alarm intent.
I/FOCUSALARM: Alarm Service has started.
I/FOCUSALARM: Notifications sent.
As our appreciated contributor @RobCo said: in Oreo+ devices you have to create a notification channel, for this use the following method as shown in the Android Developers documentation: 正如我们感谢的贡献者@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);
}
}
Then just use this same notificationManager object on your notificationBuilder 然后在您的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.");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.