首页 > 解决方案 > AlarmManager 未触发通知

问题描述

我遵循了几个关于每 24 小时设置重复通知的教程,下面是我的代码不起作用,不知道为什么。另外,我想在单击通知时调用一个方法,这就是为什么我在通知 Intent 中添加了一个额外的“NotiClick”。

public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, Menu.class);
    intent.putExtra("NotiClick",true);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.luckywheel)
            .setContentTitle("Alarm Fired")
            .setContentText("Events to be Performed").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent);
    notificationManager.notify(NotificationID.getID(), mNotifyBuilder.build());
    }
}

这是我在 OnCreate 方法中的 Menu.class 中设置闹钟的内容。

Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 13);
    calendar.set(Calendar.SECOND, 0);
    Intent intent1 = new Intent( Menu.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(Menu.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) Menu.this.getSystemService(ALARM_SERVICE);
    Log.d("NOTIFICATION", "SET " + am);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null)
        {
            //Cry about not being clicked on
        }
        else if (extras.getBoolean("NotiClick"))
        {
            //Notification clicked
        }

    }

另外,我在清单中设置了接收器:

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

另外,我的手机是华为的,如果它有一些重要性的话。我已将日历设置为比安装时间高 1 分钟,因此我可以收到通知,但什么也没发生。关于为什么这不起作用的任何想法?

标签: androidnotificationsandroid-notificationsalarmmanager

解决方案


For API >= 26, you need to set the NotificationChannel:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
        context).setSmallIcon(R.drawable.luckywheel)
        .setContentTitle("Alarm Fired")
        .setContentText("Events to be Performed").setSound(alarmSound)
        .setAutoCancel(true).setWhen(when)
        .setPriority(Notification.PRIORITY_HIGH)
        .setContentIntent(pendingIntent);

//create notification channel
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel("custom_id","custom_name",
                        NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("custom_description");
    notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(NotificationID.getID(), mNotifyBuilder.build());

Add this and see if it solves the issue.


推荐阅读