Android - 每天特定时间通知

Android - Notification at specific time every day

我正在尝试向我的应用程序添加通知功能。我希望它每天在同一时间 运行 发出通知或采取行动。我现在有这个通知代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;

public class MainActivity extends AppCompatActivity {

NotificationCompat.Builder notification;
private static final int uniqueID = 45612;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notification = new NotificationCompat.Builder(this);
    notification.setAutoCancel(true);

    // Build the notification
    notification.setSmallIcon(R.drawable.icon);
    notification.setTicker("Brook Betterment Plan");
    notification.setWhen(System.currentTimeMillis());
    notification.setContentTitle("Brook Betterment Plan");
    notification.setContentText("Don't forget to enter your daily stats! ");

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setContentIntent(pendingIntent);

    // Builds notification and issues it
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(uniqueID, notification.build());
}
}

谢谢!希望有人知道答案。另外,我希望它不需要任何额外的活动或 Java class。

创建将生成通知的 class,例如 LocalNotification

在类似 showDailyNotification

的方法下,在 class 中添加您创建的代码

现在使用 JobScheduler 并每 24 小时安排一次作业,当作业开始时在 LocalNotification 中调用此方法

将 LocalNotification 设为单例 class。

顺便说一句,不要忘记使用通知通道,否则它将无法在 Oreo 及更高版本的设备上运行。

警报管理器

public static void startAlarmBroadcastReceiver(Context context) {
    Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

报警广播接收器

AndroidManifest中,只需将class定义为

<receiver android:name=".AlarmBroadcastReceiver" >
</receiver>

代码会像

public class AlarmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    showNotification(context);

}

void showNotification(Context context) {
    String CHANNEL_ID = "your_name";// The id of the channel.
    CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
    NotificationCompat.Builder mBuilder;
    Intent notificationIntent = new Intent(context, TestActivity.class);
    Bundle bundle = new Bundle();
    notificationIntent.putExtras(bundle);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= 26) {
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
        mNotificationManager.createNotificationChannel(mChannel);
        mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLights(Color.RED, 300, 300)
                .setChannelId(CHANNEL_ID)
                .setContentTitle("Title");
    } else {
        mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle("Title");
    }

    mBuilder.setContentIntent(contentIntent);
    mBuilder.setContentText("Your Text");
    mBuilder.setAutoCancel(true);
    mNotificationManager.notify(1, mBuilder.build());
}

}