繁体   English   中英

Android Alarm Manager无法触发,尝试发送通知

[英]Android Alarm Manager not firing, trying to send Notification

我正在尝试设置一次警报,该警报将唤醒我的应用,然后向用户发送通知。

我将闹钟设置为将来1分钟,但是我的广播接收器从未被呼叫。

这就是我计划AlarmManager并构建Notification

private void scheduleNotification(Date notificationdate) {
    Log.d("PSBMF", "scheduling notification at "+notificationdate);

    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:"+AppController.statewidephone));
    PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

    String notificationmessage = "Test Message";
    String channelid = "Channel Id";
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity(), channelid)
            .setSmallIcon(R.drawable.ic_warning_black_36dp)
            .setContentTitle("Notification Title")
            .setContentText("Notification Content")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Notification Content"))
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setDefaults(Notification.DEFAULT_ALL)
            ;

    Notification notification = mBuilder.build();

    Intent notificationIntent = new Intent(mActivity, NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(mActivity, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long notificationTimeInMillis = notificationdate.getTime();
    AlarmManager alarmManager = (AlarmManager)mActivity.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);

}

我的NotificationPublisher.class看起来像这样:

public class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {

        Log.d("NP", "NOTIFICATION RECEIVED: "+context);

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);

    }
}

并通过将其放入清单的<application/>标记中来注册了NotificationPublisher/BroadcastReceiver

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

我还想念什么? 为什么我的Alarm不会被触发并调用onReceive方法?

我想我已经解决了我的问题:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);

应该

alarmManager.set(AlarmManager.RTC_WAKEUP, notificationTimeInMillis, pendingAlarmIntent);

感觉链接与广播接收器的更改有关。

由于以下原因,在清单中注册接收者将不再起作用:

注意:如果您的应用程序以API级别26或更高级别为目标,则您不能使用清单为隐式广播(不专门针对您的应用程序的广播)声明接收方,除了一些不受该限制的隐式广播。 在大多数情况下,您可以使用计划的作业。

上面的注释是从链接中复制的。

希望这可以帮助。

尝试动态注册您的广播

NotificationPublisher myBroadcastReceiver = new NotificationPublisher();// Register the broadcast
registerReceiver(myBroadcastReceiver, intentFilter);

并更改此alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,notificationTimeInMillis, pendingAlarmIntent);

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,notificationTimeInMillis, pendingAlarmIntent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM