设置日期数组的警报通知

时间:2019-06-13 10:59:32

标签: android alarmmanager localnotification

我正在尝试创建一个在给定日期之前2小时,4小时以及在给定日期之前变慢的本地通知。这是我的代码,但是不起作用:

private void alarmnotification(String notificationid, String type, long timemills) {
    Random rand = new Random();

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timemills);



    AlarmManager mgrAlarm = (AlarmManager)getSystemService(ALARM_SERVICE);
    ArrayList<PendingIntent> intentArrayd = new ArrayList<PendingIntent>();

    for(int i = 0; i < 4; ++i)
    {
        long timemfills = timemills - 7200000*i ;
        Calendar calendadr = Calendar.getInstance();
        calendadr.setTimeInMillis(timemfills);

        Calendar calendad0r = Calendar.getInstance();
        calendad0r.setTimeInMillis(SystemClock.elapsedRealtime()+calendadr.getTimeInMillis());


        Intent intent = new Intent(getApplicationContext(), NotificationPublisher.class);
        intent.putExtra("type", type);
        intent.putExtra("notificationId", notificationid);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Home.this, i, intent, 0);

        mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, timemfills, pendingIntent);

        intentArrayd.add(pendingIntent);


    }
}

这是我的通知发布者代码: 公共类NotificationPublisher扩展了BroadcastReceiver {

public static String NOTIFICATION_ID = "notificationId";
public static String NOTIFICATION = "type";
private LocalBroadcastManager broadcaster;
public void onReceive(Context context, Intent intent) {
    // Get id & message from intent.
    String notificationId = intent.getStringExtra("notificationId");
    String message = intent.getStringExtra("type");
    // When notification is tapped, call MainActivity.
    Intent mainIntent = new Intent(context, Home.class);
    mainIntent.putExtra("retour", message);
    mainIntent.putExtra("element_id", notificationId);
    mainIntent.setAction(Long.toString(System.currentTimeMillis()));
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

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

    // Prepare notification.
    Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(R.drawable.icoapp_and)
            .setContentTitle(notificationId)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setWhen(System.currentTimeMillis())
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_ALL);

    // Notify
    Random rand = new Random();
    myNotificationManager.notify(rand.nextInt(), builder.build());


}

}

问题在于我根本没有收到任何通知。

2 个答案:

答案 0 :(得分:0)

如果您运行的是Android 8.0(API级别26)或更高版本,则应创建一个通知频道以获取通知。

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

有关完整文档,请点击以下链接: Create and Manage Notification Channels

答案 1 :(得分:0)

尝试一下。因为这会触发闹钟按时发送到功能,并且在该时间的2小时之前和4小时之前

也不要忘记添加频道,因为它不能在Orea或更高版本中使用

    private void alarmnotification(String notificationid, String type, long timemills) 
    {
    Random rand = new Random();

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timemills);


    AlarmManager mgrAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    ArrayList<PendingIntent> intentArrayd = new ArrayList<PendingIntent>();
    Calendar calendadr = Calendar.getInstance();
    Intent intent;
    PendingIntent pendingIntent;
    for (int i = 0; i < 4; ++i) {
        switch (i) {
            case 1:
                // setting alarm on the specified Date
                calendadr.setTimeInMillis(timemills);
                intent = new Intent(this, NotificationPublisher.class);
                intent.putExtra("type", type);
                intent.putExtra("notificationId", notificationid);
                pendingIntent = PendingIntent.getBroadcast(Home.this, rand, intent, 0);
                mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendadr.getTimeInMillis(), pendingIntent);
                intentArrayd.add(pendingIntent);
                break;
            case 2:
                // setting alarm 2 hours earlier of the date
                calendadr.setTimeInMillis(timemills);
                // deducting 2 hours
                calendadr.set(Calendar.HOUR_OF_DAY, -2);
                intent = new Intent(this, NotificationPublisher.class);
                intent.putExtra("type", type);
                intent.putExtra("notificationId", notificationid);
                pendingIntent = PendingIntent.getBroadcast(Home.this, rand, intent, 0);
                mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendadr.getTimeInMillis(), pendingIntent);
                intentArrayd.add(pendingIntent);
                break;
            case 3:
                // setting alarm 2 hours earlier of the date
                calendadr.setTimeInMillis(timemills);
                // deducting 2 hours
                intent = new Intent(this, NotificationPublisher.class);
                calendadr.set(Calendar.HOUR_OF_DAY, -4);
                intent.putExtra("type", type);
                intent.putExtra("notificationId", notificationid);
                pendingIntent = PendingIntent.getBroadcast(Home.this, rand, intent, 0);
                mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendadr.getTimeInMillis(), pendingIntent);
                intentArrayd.add(pendingIntent);

                break;
        }

    }
}