本地通知问题

Trouble with local Notifications

最近我刚刚将我的所有代码从一个 Xamarin.Forms 项目中转移到 Android 工作室中的一个 Android 项目中,该项目具有代表警报的本地通知。安排此警报的流程如下,在 Xamarin 项目中运行良好,但不会在 Android 项目中触发:

使用控制器在此处创建警报class

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

控制器使用正确的设置成功安排了警报,并使用 AlarmManager 设置了正确的触发时间。

AlarmerReceiver 是一个自定义的 BroadcastReceiver

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

Activity 警报触发时显示

 @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 推出的 NotificationsChannel。为什么我的通知不显示?特别是因为确切的代码在几个月前就起作用了。

您需要在清单中注册接收器。

<application ...>
    <receiver android:name=".AlarmReceiver"></receiver> 
</application>

您的 Xamarin 项目中可能有这个装饰器,并在传输时将其删除?这可能解释了它在 Xamarin 中的工作原理。

[BroadcastReceiver(Enabled = true)]