Android 本地通知有问题
最近,我刚刚将我所有的代码从一个Xamarin.Forms项目转移到Android studio中的一个Android项目,该项目具有表示报警的本地通知。此警报的调度流程如下所示,在Xamarin项目中运行良好,但在Android项目中不会触发: 在此处使用控制器类创建报警Android 本地通知有问题,android,alarmmanager,android-notifications,alarm,android-alarms,Android,Alarmmanager,Android Notifications,Alarm,Android Alarms,最近,我刚刚将我所有的代码从一个Xamarin.Forms项目转移到Android studio中的一个Android项目,该项目具有表示报警的本地通知。此警报的调度流程如下所示,在Xamarin项目中运行良好,但在Android项目中不会触发: 在此处使用控制器类创建报警 //Get the time in string format with the meridian SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:
//Get the time in string format with the meridian
SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm");
String timeString = timeFormatter.format(new Date(oneTimeAlarm.getTime()));
SimpleDateFormat meridianFormatter = new SimpleDateFormat("a");
String meridianString = meridianFormatter.format(new Date(oneTimeAlarm.getTime()));
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
//Provide Settings
intent.putExtra("Vibrate", alarm.getVibrateOn());
intent.putExtra("Id", alarm.getId());
intent.putExtra("Snooze", alarm.getSnoozeLength());
intent.putExtra("Time",timeString);
intent.putExtra("Meridian",meridianString);
intent.putExtra("Uri", alarm.getRingtoneURI());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm.getTime(),pendingIntent);
Toast.makeText(context,"Alarm Scheduled!",Toast.LENGTH_SHORT).show();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
setContentView(R.layout.activity_alarm);
initialize();
...
控制器使用正确的设置成功调度警报,并使用AlarmManager设置正确的触发时间
AlarmerReceiver,它是一个自定义广播接收器
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
//Get values for alarm
Boolean vibrate = intent.getBooleanExtra("Vibrate",false);
String ringtoneURI = intent.getStringExtra("Uri");
int id = intent.getIntExtra("Id",0);
int snoozeLength = intent.getIntExtra("Snooze",5);
String displayTime = intent.getStringExtra("Time");
String displayMeridian = intent.getStringExtra("Meridian");
//If this is an update cancel the original
Intent cancel = new Intent("Dismiss");
PendingIntent cancelPending = PendingIntent.getBroadcast(context,id,cancel,PendingIntent.FLAG_CANCEL_CURRENT);
//Pass parameters to AlarmActivity so it can have same settings for snooze refire.
Intent alarmIntent = new Intent(context,AlarmActivity.class);
alarmIntent.putExtra("SNOOZE",snoozeLength);
alarmIntent.putExtra("VIBRATE",vibrate);
alarmIntent.putExtra("URI",ringtoneURI);
alarmIntent.putExtra("ID",id);
alarmIntent.putExtra("TIME",displayTime);
alarmIntent.putExtra("MERIDIAN",displayMeridian);
PendingIntent pendingAlarmIntent = PendingIntent.getActivity(context,id,alarmIntent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//Build Notification
builder.setCategory(Notification.CATEGORY_ALARM)
.setSmallIcon(R.drawable.ic_home_black_24dp)
.setFullScreenIntent(pendingAlarmIntent,true)
.setContentIntent(pendingAlarmIntent)
.setContentTitle("NapChat Alarm")
.setContentText("Open Alarm")
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_MAX)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(id,builder.build());
警报触发时显示的活动
//Get the time in string format with the meridian
SimpleDateFormat timeFormatter = new SimpleDateFormat("hh:mm");
String timeString = timeFormatter.format(new Date(oneTimeAlarm.getTime()));
SimpleDateFormat meridianFormatter = new SimpleDateFormat("a");
String meridianString = meridianFormatter.format(new Date(oneTimeAlarm.getTime()));
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
//Provide Settings
intent.putExtra("Vibrate", alarm.getVibrateOn());
intent.putExtra("Id", alarm.getId());
intent.putExtra("Snooze", alarm.getSnoozeLength());
intent.putExtra("Time",timeString);
intent.putExtra("Meridian",meridianString);
intent.putExtra("Uri", alarm.getRingtoneURI());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm.getTime(),pendingIntent);
Toast.makeText(context,"Alarm Scheduled!",Toast.LENGTH_SHORT).show();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
setContentView(R.layout.activity_alarm);
initialize();
...
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmsproject.alarmsprojectandroid">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activities.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.SignUpActivity" />
<activity
android:name=".Activities.HomeActivity"
android:label="@string/title_activity_home" />
<activity android:name=".Activities.OptionsActivity" />
<activity
android:name=".Activities.AlarmActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_alarm"
android:theme="@style/FullscreenTheme" />
<activity android:name=".Activities.CreateAlarmActivity"></activity>
</application>
</manifest>
有人能看出我哪里出了问题,为什么它不会开火吗。我的目标是API级别25,以避免使用针对API 26的NotificationsAnnel。为什么我的通知不显示?特别是因为确切的代码在几个月前就已经生效了 您需要在清单中注册接收者
<application ...>
<receiver android:name=".AlarmReceiver"></receiver>
</application>
粘贴您的清单,以及警报已被更改,以这样的方式,他们可以延迟,这是由于捆绑。另外,您是否通过查看您通过日志将报警设置为的时间来验证您的报警设置是否正确?是的,我确保在使用报警管理器进行设置时,将时间转换为utc毫秒,因为报警对象时间被保存为utc毫秒,并在setExact()方法中使用。您是否使用直接回调重载进行了尝试?它工作吗?我知道有两个独立的步骤(触发警报,然后显示通知),第一个不工作。如果是这样,
adb shell dumpsys alarm
可能有用,这肯定是我遗漏的东西,但通知仍然没有发出。我刚刚看到,在“设置”>“应用程序和通知”>“MyApp”中,权限部分显示“未请求任何权限”。即使此应用程序的通知已打开,这可能会阻止它们显示吗?此外,我刚刚尝试使用模拟器上的内置时钟应用程序安排一个闹钟,它发布了一个Toast,表示它试图在不使用频道的情况下安排通知。这似乎很奇怪,因为默认系统不会使用正确的API吗?