如何在Android上正确发送重复通知

时间:2019-07-03 15:51:00

标签: android notifications

我在Android上设置了重复通知,但我的设备上没有出现警报。

我已经阅读了Android Developers文档,根据它,我的代码看起来还不错,但仍然无法正常运行。我正在使用一个类作为 BroadcastReceiver ,该类从MainActivity接收意图,然后将其传递给 IntentService

这是在MainActivity上触发警报的方法,它在每次应用程序初始化时运行。

implementation

这是使用BroadcastReceiver的类

api

这是使用IntentService的类

 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.");

        }

当我运行它时,它会显示所有日志,但通知不会显示在设备上。

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);

    }

}

1 个答案:

答案 0 :(得分:1)

正如我们感激的贡献者@RobCo所说:在Oreo +设备中,您必须创建一个通知通道,为此,请使用Android开发人员文档中所示的以下方法:

Dim Rangedata as Range, U as Range
'Table Range
Set Rangedata = D.Range("A1:C" & Range("A1").End(xlDown).Row)
Rangedata.AutoFilter Field:=2, Criteria1:=Log.Cells(8, 3) 
'Amount which i want to copy without Header
Set U = D.Range("C1:C" & Range("C1").End(xlDown).Row)
U.Offset(1, 0).SpecialCells(xlCellTypeVisible).Select
U.copy

然后只需在您的notificationBuilder上使用相同的notificationManager对象

 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);
        }
    }