难以为Android重复发送通知编码吗?

时间:2013-03-14 22:19:58

标签: android notifications push

我想每隔12小时向用户发送一条通知消息,但我无法确定从哪里开始。有人可以提供一个分步指南,向我的用户添加一个简单的通知吗?提前致谢

2 个答案:

答案 0 :(得分:1)

步骤是:

  • 通过AlarmManager
  • 设置闹钟
  • 在报警火的BroadcastReceiver上填充Notification,然后您可以在12h内设置下一个闹钟

另一种方法是从头开始每12小时重复一次。

请参阅此示例AlarmManager and Notification in Android

答案 1 :(得分:0)

您可以使用此代码:

private static final int TIME = 1000*60*60*12;

        new Timer().scheduleAtFixedRate(new TimerTask() {
            public void run() {
                showNotification();
            }
        }, 0, TIME);//start immediatly, run every 12hours


    public void showNotification() {
        final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final Intent launchNotifiactionIntent = new Intent(this, ActivityLauchedOnClickNotif.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this,
                    REQUEST_CODE, launchNotifiactionIntent,
                    PendingIntent.FLAG_ONE_SHOT);

        Notification.Builder builder = new Notification.Builder(this)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(titleString)
            .setContentText(messageString)
            .setContentIntent(pendingIntent);

        mNotification.notify(NOTIFICATION_ID, builder.build());
    }

此代码自API 11起作用! (notification.builder)