如何在每48小时后显示android通知?

时间:2014-03-06 20:06:29

标签: android android-layout notifications push-notification

我尝试使用Android通知代码,但是当我启动Android应用程序时,它会给我通知弹出窗口。

  

我想在每48小时后显示一个通知,我该怎么办?

为了使其发挥作用,我需要做出哪些改变?

通知代码

 Intent notificationIntent = new Intent(MainActivity.this, Activity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

         NotificationManager notificationManager = (NotificationManager) MainActivity.this
                    .getSystemService(Context.NOTIFICATION_SERVICE);

         Notification noti = new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setTicker("ticker message")

                            .setWhen(System.currentTimeMillis())
                            .setContentTitle("HELLO")
                            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
                            .setContentIntent(contentIntent)
                            //At most three action buttons can be added
                            .setAutoCancel(true).build();
        int notifyID =0;
        notificationManager.notify(notifyID, noti);

3 个答案:

答案 0 :(得分:12)

这里有一些与您主要活动中所需内容相近的内容:

    Intent notificationIntent = new Intent(context, ShowNotification.class);
    PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent,
                                                           PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.cancel(contentIntent);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
            + AlarmManager.INTERVAL_DAY * 2, AlarmManager.INTERVAL_DAY * 2, contentIntent);

然后,在另一个名为ShowNotification.java的文件中,添加以下内容(假设您的主要活动名为MainActivity):

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class ShowNotification extends Service {

    private final static String TAG = "ShowNotification";

    @Override
    public void onCreate() {
        super.onCreate();

        Intent mainIntent = new Intent(this, MainActivity.class);

        NotificationManager notificationManager
            = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification noti = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("HELLO " + System.currentTimeMillis())
            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("ticker message")
            .setWhen(System.currentTimeMillis())
            .build();

        notificationManager.notify(0, noti);

        Log.i(TAG, "Notification created");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

答案 1 :(得分:1)

这样做: - 您的通知将在活动开始时打开。 - 如果您添加了计时器,则仅在您打开应用程序时才会显示。

在你的情况下,你应该开始一项服务,你将有一个计时器,每48小时向Androis O.S发送一个通知。

要执行此循环,您需要一个timmer,您可以在此处查看有关服务的更多信息: http://www.vogella.com/tutorials/AndroidServices/article.html

答案 2 :(得分:1)

您可能希望使用AlarmManager类来安排通知。这是一个应该执行此操作的片段:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(contentIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                                 System.currentTimeMillis() + AlarmManager.INTERVAL_DAY * 2,
                                 AlarmManager.INTERVAL_DAY * 2,
                                 contentIntent);