为日期数组设置警报通知

Posted

技术标签:

【中文标题】为日期数组设置警报通知【英文标题】:Setting alarm notification for Dates Arrays 【发布时间】:2019-06-13 10:59:32 【问题描述】:

我正在尝试创建本地通知,该通知在给定日期之前 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());



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

【问题讨论】:

可以添加 NotificationPublisher 代码吗? 这是我的接收方法: PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0); NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 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); 随机 rand = new Random(); myNotificationManager.notify(rand.nextInt(), builder.build()); 【参考方案1】:

如果您在 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

【讨论】:

问题不在于android版本,因为我确实收到了通知。当我删除循环时, 好的,这是另一个问题,但至少您需要检查您的用户是否运行 Android 8.0 或更高版本。顺便说一句,尝试记录timemfills 的结果并在此网站currentmillis 上转换它。看看时间是否设置正确。 我的问题是:我有一组面试日期,我必须在每个日期前 6 小时提醒用户,然后在两个小时前 4 小时提醒用户。任何帮助。 你试过记录时间吗?确保在 4 小时和 6 小时之前正确设置。 我做的第一件事,我将时间转换为日期并在日志中打印它是正确的,程序进入循环但它没有发送通知,我不知道为什么, 【参考方案2】:

尝试一下。因为这将在时间发送到功能时触发警报,并且在该时间的 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;
        

    

【讨论】:

我评论了intentArrayd.add(pendingIntent)的部分;我在rand的地方设置了0,它的工作正常,

以上是关于为日期数组设置警报通知的主要内容,如果未能解决你的问题,请参考以下文章

如何为特定的星期几设置重复通知?

ios本地通知同时通知但不同日期的不同警报消息

在本地通知中设置repeatInterval

制作通知提醒样式

在本地通知 iOS 上每天在警报正文中设置随机消息的问题

我可以从我自己的应用程序创建提醒/警报类型的通知吗?