创建android警报并获取通知

tct7dpnv  于 2022-12-02  发布在  Android
关注(0)|答案(2)|浏览(128)

我可以设置日期,时间,并键入任何要求,我不会成功和提醒。我没有收到任何错误,但我没有收到任何通知,当任务设置。这些是我的代码片段如下:
这是我设置闹钟类:
如果您有任何问题,请使用下面的语句:

//create an intent to show notification

Intent =新建Intent(创建任务. this,任务通知报警. class);("事件",文本);("时间",日期);("日期",时间);
如果您有任何问题,请联系我们。如果您有问题,请联系我们。字符串日期和时间=日期+""+时间通知;日期格式格式化程序=新的简单日期格式("d-M-yyyy hh:mm"); try {日期1 =格式化程序. parse(日期和时间);设置报警管理器。(),"闹钟",Toast. LENGTH_SHORT).().如果您有任何异常,请使用以下语句:}
如果您想创建一个新的Intent,请使用getApplicationContext()。活动新任务|目的标志_活动_清除_任务);启动活动(返回意图);}

this is my notification class:

{1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} {14} {15} {16} {17} {18} {19} {20} {21} {22} {23} {24} {25} {26} {27} {28} {29} {30} {31} {32} {33} {34} {35} {36} {37} {38} {39} {39} {40} {41} {42} {43} {44} {45} {46} {47} {48} {49} {40} {49} {40} {41} {42} {43} {44} {45} {46} {47} {48} {49} {50} {51} {52字符串text = bundle. getString("事件");字符串描述= bundle.getString("事件描述");字符串日期= bundle.getString("日期")+""+ bundle.getString("时间");
Intent intent1 =新建Intent(上下文,警报详细信息.类);如果是,则将其设置为0。("消息",文本);
如果您有一个活动,那么您就可以使用该活动的上下文。如果您的系统没有运行,请单击"通知管理器"。NotificationCompat.Builder构建器=新建NotificationCompat.Builder(上下文,"通知_001");
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.activity_notification); PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, intent, 0); contentView.setOnClickPendingIntent(R.id.flashButton, pendingSwitchIntent); contentView.setTextViewText(R.id.message, text); contentView.setTextViewText(R.id.date, date); builder.setSmallIcon(R.drawable.ic_baseline_calendar); builder.setAutoCancel(true); builder.setOngoing(true); builder.setAutoCancel(true); builder.setPriority(Notification.PRIORITY_HIGH); builder.setOnlyAlertOnce(true); builder.build().flags = Notification.FLAG_NO_CLEAR | Notification.PRIORITY_HIGH; builder.setContent(contentView); builder.setContentIntent(pendingIntent);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { String channelId = "channel_id"; NotificationChannel channel = new NotificationChannel(channelId, "channel name", NotificationManager.IMPORTANCE_HIGH); channel.enableVibration(true); notificationManager.createNotificationChannel(channel); builder.setChannelId(channelId); } Notification notification = builder.build(); notificationManager.notify(1, notification); } }

qxsslcnc

qxsslcnc1#

程序员同事。
我认为问题在于通知通道无法识别构建器。
我建议你先创建通知通道,然后创建构建器,在文件中全局声明通道的变量。
此外,您还应将通知通道创建代码和通知生成器代码分成两个单独的方法,并在广播接收器中的onReceive()(TaskNotificationAlarm)中调用它们,例如

private void createNotificationChannel() {
  // Write your channel creation code here
  Log.d("TAG","Notification Channel Running")
}
private void createNotification() {
// Write your notification builder code here
Log.d("TAG","Notification Builder Running")
}

不要忘记在清单文件中提及广播接收器类,如下所示:

<receiver
        android:name=".TaskNotificationAlarm"
        android:enabled="true"
        android:exported="false" />

请确保在<application></application>标记中提及此接收器。

  • 尝试引发如代码部分所示的日志,以验证是否正在调用该方法。
  • 在激发BroadcastReceiver代码的位置启动调试点。

尝试使用报警管理器的setAndAllowWhileIdle()方法触发报警,此方法将允许操作系统触发报警,即使设备处于节电模式或低功耗模式。

private AlarmManager alarmManager;
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setAndAllowWhileIdle (int type, 
            long triggerAtMillis, 
            PendingIntent operation)

要取消警报,只需使用预定义的alarmManager.cancel()方法
有关Alarm Manager的详细信息,请单击“我”

56lgkhnf

56lgkhnf2#

alarmManager.set将在近似时间而非精确时间触发警报。为了显示精确警报,您需要使用
alarmManager.setAlarmClock()
有关更多详细信息,请查看Schedule alarms

相关问题