本地通知有问题

问题描述 投票:1回答:1

最近我刚从Xamarin.Forms项目转移了我的所有代码,该项目在Android工作室中向Android项目发送了代表警报的本地通知。调度此警报的流程如下,并且在Xamarin项目中工作正常,但不会在Android项目中触发:

使用Controller类在此创建警报

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

Controller使用正确的设置成功调度警报,并使用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());

报警触发时显示的活动

 @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。为什么我的通知不显示?特别是因为确切的代码几个月前工作。

android alarmmanager android-notifications alarm android-alarms
1个回答
1
投票

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

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

您可能在Xamarin项目中有这个装饰器,并在转移时将其删除?这可能解释它在Xamarin中是如何工作的。

[BroadcastReceiver(Enabled = true)]
© www.soinside.com 2019 - 2024. All rights reserved.