[英]How to send repeating notifications properly on Android
我在Android上設置了重復通知,但“鬧鍾”未出現在我的設備上。
我已經閱讀了Android Developers文檔,根據它,我的代碼看起來還不錯,但仍然無法正常運行。 我正在使用一個類作為BroadcastReceiver ,它從MainActivity接收意圖,然后將其傳遞給IntentService 。
這是在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.");
}
這是使用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.");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.