【问题标题】:Android Notification at time安卓通知时间
【发布时间】:2013-06-11 21:17:46
【问题描述】:

我现在正在寻找几个小时如何准确地做到这一点:

我希望每天(周末除外)一次发送通知(比如 18:00(= 下午 6 点)),除非应用程序已经打开。它基本上就像您收到邮件时的 gmail 应用程序。 当用户点击通知时,它应该会消失并被带到 MainActivity。

我用 AlarmManager 尝试了很多东西,但没有一个会导致显示通知。

我尝试过的代码如下: 在我的 MainActivity 中:

AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

我的通知服务:

public class NotificationService extends IntentService {



    public NotificationService() {
        super("NotificationService");
    }

    @Override
    @SuppressWarnings("deprecation")
    protected void onHandleIntent(Intent intent) {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher, "reminder", System.currentTimeMillis());
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
        nm.notify(1, notification);
    }
}

请注意,我使用了已弃用的东西,因为这甚至是在不使用 AlarmManager 时出现通知的唯一方式。如果可能,请使用其中没有已弃用内容但具有最新内容的解决方案:P。

非常感谢提前!!!

最诚挚的问候

【问题讨论】:

    标签: android notifications alarmmanager android-notifications


    【解决方案1】:

    终于找到了解决办法:

    private void handleNotification() {
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);
    }
    

    这是我的自定义BroadcastReceiver

    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Calendar now = GregorianCalendar.getInstance();
            int dayOfWeek = now.get(Calendar.DATE);
            if(dayOfWeek != 1 && dayOfWeek != 7) {
                NotificationCompat.Builder mBuilder = 
                        new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(context.getResources().getString(R.string.message_box_title))
                        .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date));
                Intent resultIntent = new Intent(context, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(1, mBuilder.build());
            }
        }
    }
    

    在应用程序标签中的清单中:

    <receiver
            android:name="be.menis.timesheet.service.AlarmReceiver"
            android:process=":remote" />
    

    【讨论】:

    • 这是什么android:name="be.menis.timesheet.service.AlarmReceiver"??我想在每天上午 10 点设置通知
    • @ashish 即应用包名和接收方位置
    • 感谢您的代码,我现在正在测试它。但是有谁知道我是否会在每次运行此代码时设置一个新警报?如果是这样,我应该非常小心......
    • 即使关闭应用程序也会运行吗?
    • 是的,它曾经运行过。但是,如您所见,这已经是很久以前的事了,所以我不能 100% 确定应用程序是否仍在后台运行,或者它是否实际上已关闭。我相信它真的被关闭了:)。
    猜你喜欢
    • 1970-01-01
    • 2011-07-20
    • 2017-12-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多