java - 使用警报管理器安排通知
问题描述
我知道以前有人问过这个问题,但我仍然没有找到明确的答案,因为以前的答案相当陈旧。我遵循了一些教程,尝试了几种不同的方式来显示通知。没有任何效果,我认为我的问题是我无法理解如何使用通知渠道。我遵循的所有教程都来自奥利奥之前,所以我没有看到任何有用的东西,谷歌开发者片段也没有帮助。
所以我遵循(或者更确切地说是复制)的上一个教程说要这样做:这个类使用警报管理器提醒构造通知
public class NotificationService {
public static final int DAILY_REMINDER_REQUEST_CODE = 100;
public static final String TAG = "Service";
private static Context context;
public static void setContext(Context inContext) {
context = inContext;
}
public static void setReminder(Context context, Class<?> cls, int hour, int min) {
Calendar calendar = Calendar.getInstance();
Calendar setcalendar = Calendar.getInstance();
setcalendar.set(Calendar.HOUR_OF_DAY, hour);
setcalendar.set(Calendar.MINUTE, min);
setcalendar.set(Calendar.SECOND, 0);
// cancel already scheduled reminders
cancelReminder(context, cls);
if (setcalendar.before(calendar))
setcalendar.add(Calendar.DATE, 1);
// Enable a receiver
ComponentName receiver = new ComponentName(context, cls);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, cls);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
DAILY_REMINDER_REQUEST_CODE, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
public static void cancelReminder(Context context, Class<?> cls) {
// Disable a receiver
ComponentName receiver = new ComponentName(context, cls);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Intent intent1 = new Intent(context, cls);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
pendingIntent.cancel();
}
public static void showNotification(Context context, Class<?> cls, String title, String content) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(context, cls);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(cls);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(DAILY_REMINDER_REQUEST_CODE, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, createNotificationChannel());
Notification notification = builder.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true)
.setSound(alarmSound)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setChannelId("10")
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(DAILY_REMINDER_REQUEST_CODE, notification);
}
该类是触发通知的广播接收器
public class NotifyPublisher extends BroadcastReceiver {
public static final String TAG = "Publisher";
private SharedPreferences SP;
@Override
public void onReceive(Context context, Intent intent) {
SP = context.getSharedPreferences("com.STIRlab.ema_diary", Context.MODE_PRIVATE);
if (intent.getAction() != null && context != null) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// Set the alarm here.
Log.d(TAG, "onReceive: BOOT_COMPLETED");
int hour = SP.getInt("hour", 0);
int minute = SP.getInt("minute", 0);
NotificationService.setReminder(context, NotifyPublisher.class, hour, minute);
return;
}
}
Log.d(TAG, "onReceive: ");
//Trigger the notification
NotificationService.showNotification(context, MainActivity.class,
"30 Days", "Don't forget your daily journal entry");
}
}
这就是我试图在我的 MainActivity 中触发它的方式
SP.edit().putInt("hour", 14).apply();
SP.edit().putInt("minute", 06).apply();
NotificationService.setReminder(MainActivity.this, NotifyPublisher.class, 14,06);
是的,我在清单中注册了它
<receiver android:name=".Helpers.NotifyPublisher"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
什么都没有出现,我不知道为什么。
解决方案
推荐阅读
- algorithm - 在圆柱体内创建随机大小的球体并随机连接它们
- node.js - AssertError:预期调用一次,但调用了 0 次
- c++ - 数组的高效间接排序
- python - 将训练准确率更新到前端
- mysql - 在 MySQL 表中重建后 auto_increment 列仍然不连续
- angular - 如何从角度 4 的下拉菜单中设置选定的值
- javascript - how to change select box value in jquery or javascript
- xamarin - 使用从 Nib 创建的 MvxTableViewCell 时,我应该在哪里放置初始化/绑定逻辑?
- jquery - No 'Access-Control-Allow-Origin' header is present error using bigcommerce api mvc
- datatable - 带有长查询字符串的 404