【问题标题】:Android: Sending user a notification 7 days before a certain date.Android:在某个日期前 7 天向用户发送通知。
【发布时间】:2017-06-06 09:17:29
【问题描述】:

我正在尝试创建一个在指定日期前 7 天发送 android 通知的应用程序。现在,我正在阅读正确的日期并正确设置日历,但由于某种原因,警报没有触发。此外,当我执行adb shell dumpsys alarm 以查看是否设置了警报时,转储不会显示与我的应用程序相关的任何警报。我现在创建 AlarmManager 和通知的代码是

calendar.set(year, month, day, hour, min);
calendar.add(Calendar.DAY_OF_MONTH, -7);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
Intent intent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

这可能出了什么问题?这不是设置警报的正确方法吗?我的想法来自here

【问题讨论】:

  • 您可以参考thisthis one。这不是您问题的答案,但您可以从中获得一些想法。您需要使用此参考对您的代码进行一些更改,它工作正常。
  • 您确定转储不显示警报吗?我尝试了完全相同的代码,并且在我的转储中警报显示正确?
  • 这不是从活动中调用的吗?我必须传递上下文才能执行此代码。

标签: android alarmmanager android-notifications


【解决方案1】:

我正在做以下事情: 使用getNotification() 和您的calandar.getTime() 作为scheduleNotification 的参数

private void scheduleNotification(Notification notification, Date alarmTime) {

    Intent notificationIntent = new Intent(this, NotificationReceiver.class);
    notificationIntent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTime(), pendingIntent);
}

private Notification getNotification() {
    int colour = getNotificationColour();
    Bitmap largeNotificationImage = getLargeNotificationImage();
    return new RandomNotification(this).getNotification(
            "Text",
            "More text",
            getNotificationImage(),
            largeNotificationImage,
            colour);
}

private int getNotificationColour() {
    return ContextCompat.getColor(this, R.color.colorAccent);
}

private Bitmap getLargeNotificationImage() {
    return BitmapFactory.decodeResource(this.getResources(),
            R.mipmap.ic_launcher);
}

我的 RandomNotification 类:

public class RandomNotification {

    private Context context;

    public RandomNotification(Context context) {
        this.context = context;
    }

    public Notification getNotification(String title, String message, int imageResourceId, Bitmap largeResource, int colour) {

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(imageResourceId)
                .setLargeIcon(largeResource)
                .setTicker("Ticker")
                .addAction(0, "Button", getButtonIntent())
                .setSound(soundUri)
                .setLights(Color.BLUE, 300, 100)
                .setAutoCancel(true)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(getPendingNotificationIntent());

        handleNotificationColor(builder, colour);

        Notification notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
        return notification;
    }

    private Notification.Builder handleNotificationColor(Notification.Builder builder, int colour) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setColor(colour);
        }
        return builder;
    }

    private PendingIntent getPendingNotificationIntent() {
        Intent notificationIntent = new Intent(context, HomeActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 1,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingNotificationIntent;
    }

    private PendingIntent getButtonIntent() {
        Intent notificationButtonReceiver = new Intent(context, NotificationButtonReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationButtonReceiver, PendingIntent.FLAG_CANCEL_CURRENT);
        return pendingIntent;
    }

}

我的通知按钮接收类:

public class NotificationButtonReceiver extends BroadcastReceiver {



    private Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        // do something

    }


}

我更新的通知接收器类

public class NotificationReceiver extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";

    public static String NOTIFICATION = "notification";

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

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

我的清单文件:

<receiver
    android:name=".NotificationButtonReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.NOTIFY" />
    </intent-filter>
</receiver>
    <receiver
        android:name=".NotificationReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>

【讨论】:

  • NotificationReceiver.NOTIFICATION_ID 来自哪里?
  • 这似乎设置了警报。我忘了在清单文件中添加权限
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
相关资源
最近更新 更多