Android-每天的特定时间通知

时间:2018-07-25 03:48:13

标签: java android notifications timed

我正在尝试向我的应用程序添加通知功能。我希望它每天都在同一时间运行通知或操作。我现在有以下代码可用于通知:

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

谢谢!希望有人知道答案。另外,我希望它不需要任何其他的Activity或Java类。

2 个答案:

答案 0 :(得分:0)

Create a class that will generate notification, like LocalNotification

Add the code you have in on create in that class under a method like showDailyNotification

Now use JobScheduler and schedule a job for every 24 hours, when the job is started call this method in LocalNotification

Make LocalNotification a singleton class.

BTW don't forget to use notification channel because otherwise it will not work on Oreo and above devices.

答案 1 :(得分:0)

警报管理器

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中,只需将类定义为

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

}