【问题标题】:How to show android notification after every 48hours?如何每 48 小时后显示 android 通知?
【发布时间】:2014-03-06 20:06:29
【问题描述】:

我尝试了以下 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);

【问题讨论】:

标签: android android-layout notifications push-notification


【解决方案1】:

以下是您在主要活动中所需要的内容:

    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;
    }
}

【讨论】:

  • 如何设置每隔一小时?
  • 我想显示应用是否打开的通知
  • setRepeating() 方法的第二个和第三个参数分别指定了初始警报的时间和后续警报的间隔。您要使用的单位是 AlarmManager.INTERVAL_HOUR。但正如 developer.android.com/reference/android/app/…, long, long, android.app.PendingIntent 的文档中所述,如果您使用 API19 及更高版本,这种方法将不再适用于较短的持续时间。
  • 您希望从“AlarmManager.INTERVAL_DAY * 2”位中删除“* 2”。
【解决方案2】:

这样做: - 您的通知将在您的活动开始时打开。 - 如果你添加了一个计时器,它只会在你打开应用程序时出现。

在您的情况下,您应该启动一项服务,其中您将有一个计时器,每 48 小时向 Androis O.S. 发送一次通知

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

【讨论】:

    【解决方案3】:

    您可能希望使用 AlarmManager 类来安排通知。这是一个应该这样做的sn-p:

    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);
    

    【讨论】:

    • 谢谢Intent notificationIntentNotification 代码在哪里?
    • 在您的待处理意图中,您需要指定一个单独的公共类来创建和执行您的通知代码。您可以使用已有的基本通知代码。
    • 感谢@scottt,我是 Android 开发人员的新手,能否请您更新您的答案,这将对您有很大帮助。
    • 点击通知应用程序没有打开?
    猜你喜欢
    • 1970-01-01
    • 2022-10-25
    • 2022-01-21
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多