我试了一下,通知应该在指定的时间发给我?
I tried and saw that the notification should come to me at the specified time?
我试了一下,通知应该在指定的时间发给我,结果没发给我
.setContentTitle(title.get("通知标题"))
.setContentText(subText.get("通知的子文本"))
标题潜台词错误
通知应该只在指定时间发给我 我应该只在我放置通知时收到通知
public class MainActivity extends AppCompatActivity {
@RequiresApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationCheckPoint();
}
@RequiresApi(Build.VERSION_CODES.N)
private void NotificationCheckPoint() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 53);
calendar.set(Calendar.SECOND, 0);
Intent MyIntent = new Intent(getApplicationContext(), BroadastNotification.class);
PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,
MyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
MyAlarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, MyPendIntent);
}
这个广播通知
public class BroadcastNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
sendNotification(context);
}
private void sendNotification(Context context) {
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "101";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
notificationChannel.setDescription("Game Notifications");
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{200});
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title.get("Title of notification"))
.setContentText(String.substring("Sub text of notification"))
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(1, notificationBuilder.build());
}
您的要求是使用 AlarmManager
基本上每天在固定时间显示通知。
这就是我在早期项目中实现它的方式
BroacastReceiver.java:-
public class BroadcastNotification extends BroadcastReceiver {
private int notificationID = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (context != null && intent != null) {
showNotification(context);
}
}
private Notification createNotification(@NonNull Context context) {
String NOTIFICATION_CHANNEL_ID = "101";
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(SharedPreferenceHelper.getTitle(context))
.setContentText(SharedPreferenceHelper.getContent(context))
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setSound(defaultSound)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
.setStyle(NotificationCompat.BigTextStyle()
.bigText(localNotification.content));
return notificationBuilder.build();
}
private void showNotification(Context context) {
Notification notification = createNotification(context, localNotification)
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Notification",
NotificationManager.IMPORTANCE_MAX);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationID, notification);
notificationID++;
}
NotificationScheduler.java
public class NotificationScheduler {
@Nullable
public static String scheduleRepeatingNotification(@NonNull Activity activity) {
setNotificationData(activity, "Title of notification", "Sub text of notification")
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 53);
calendar.set(Calendar.SECOND, 0);
PendingIntent MyPendIntent = makePendingIntent(activity);
AlarmManager MyAlarm = (AlarmManager) activity.getSystemService(ALARM_SERVICE);
MyAlarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, MyPendIntent);
}
public static PendingIntent makePendingIntent(Activity activity) {
Intent myIntent = new Intent(activity, BroadastNotification.class);
// If you have multiple notifications, you'll have to update the requestCode accordingly. its 100 in this case.
return PendingIntent.getBroadcast(activity, 100,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}
SharedPreferenceHelper.java :-
public class SharedPreferenceHelper {
@Nullable
public static String getNotificationTitle(@NonNull Context context) {
SharedPreferences sp = context.getSharedPreferences("NotificationSharedPref", Context.MODE_PRIVATE);
return sp.getString("notification-title", null);
}
@Nullable
public static String getNotificationContent(@NonNull Context context) {
SharedPreferences sp = context.getSharedPreferences("NotificationSharedPref", Context.MODE_PRIVATE);
return sp.getString("notification-content", null);
}
public void setNotificationData(@NonNull Context context, String title, String content) {
SharedPreferences sp = context.getSharedPreferences("NotificationSharedPref", Context.MODE_PRIVATE);
sp.edit().putString("notification-title", title).apply();
sp.edit().putString("notification-content", content).apply();
}
}
注:-
这是一个不完整的解决方案,需要大量额外的代码来处理所有场景。
其一,当设备重启时,AlarmManager 被清除,因此您必须处理此广播接收器中的 ACTION_BOOT_COMPLETED
、ACTION_LOCKED_BOOT_COMPLETED
、ACTION_REBOOT
和 ACTION_MY_PACKAGE_REPLACED
事件。
或按照此 link 使用 WorkManager
-> https://martian.ventures/mantra/insights/schedule-local-notifications-on-android/
实施 LocalNotifications
如果发现任何语法错误,请更正。
我试了一下,通知应该在指定的时间发给我,结果没发给我 .setContentTitle(title.get("通知标题")) .setContentText(subText.get("通知的子文本")) 标题潜台词错误
通知应该只在指定时间发给我 我应该只在我放置通知时收到通知
public class MainActivity extends AppCompatActivity {
@RequiresApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationCheckPoint();
}
@RequiresApi(Build.VERSION_CODES.N)
private void NotificationCheckPoint() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 53);
calendar.set(Calendar.SECOND, 0);
Intent MyIntent = new Intent(getApplicationContext(), BroadastNotification.class);
PendingIntent MyPendIntent = PendingIntent.getBroadcast(getApplicationContext(), 100,
MyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager MyAlarm = (AlarmManager) getSystemService(ALARM_SERVICE);
MyAlarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, MyPendIntent);
}
这个广播通知
public class BroadcastNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
sendNotification(context);
}
private void sendNotification(Context context) {
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "101";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
@SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
notificationChannel.setDescription("Game Notifications");
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{200});
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title.get("Title of notification"))
.setContentText(String.substring("Sub text of notification"))
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX);
notificationManager.notify(1, notificationBuilder.build());
}
您的要求是使用 AlarmManager
基本上每天在固定时间显示通知。
这就是我在早期项目中实现它的方式
BroacastReceiver.java:-
public class BroadcastNotification extends BroadcastReceiver {
private int notificationID = 0;
@Override
public void onReceive(Context context, Intent intent) {
if (context != null && intent != null) {
showNotification(context);
}
}
private Notification createNotification(@NonNull Context context) {
String NOTIFICATION_CHANNEL_ID = "101";
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(SharedPreferenceHelper.getTitle(context))
.setContentText(SharedPreferenceHelper.getContent(context))
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setSound(defaultSound)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_MAX);
.setStyle(NotificationCompat.BigTextStyle()
.bigText(localNotification.content));
return notificationBuilder.build();
}
private void showNotification(Context context) {
Notification notification = createNotification(context, localNotification)
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Notification",
NotificationManager.IMPORTANCE_MAX);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationID, notification);
notificationID++;
}
NotificationScheduler.java
public class NotificationScheduler {
@Nullable
public static String scheduleRepeatingNotification(@NonNull Activity activity) {
setNotificationData(activity, "Title of notification", "Sub text of notification")
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 53);
calendar.set(Calendar.SECOND, 0);
PendingIntent MyPendIntent = makePendingIntent(activity);
AlarmManager MyAlarm = (AlarmManager) activity.getSystemService(ALARM_SERVICE);
MyAlarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, MyPendIntent);
}
public static PendingIntent makePendingIntent(Activity activity) {
Intent myIntent = new Intent(activity, BroadastNotification.class);
// If you have multiple notifications, you'll have to update the requestCode accordingly. its 100 in this case.
return PendingIntent.getBroadcast(activity, 100,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}
SharedPreferenceHelper.java :-
public class SharedPreferenceHelper {
@Nullable
public static String getNotificationTitle(@NonNull Context context) {
SharedPreferences sp = context.getSharedPreferences("NotificationSharedPref", Context.MODE_PRIVATE);
return sp.getString("notification-title", null);
}
@Nullable
public static String getNotificationContent(@NonNull Context context) {
SharedPreferences sp = context.getSharedPreferences("NotificationSharedPref", Context.MODE_PRIVATE);
return sp.getString("notification-content", null);
}
public void setNotificationData(@NonNull Context context, String title, String content) {
SharedPreferences sp = context.getSharedPreferences("NotificationSharedPref", Context.MODE_PRIVATE);
sp.edit().putString("notification-title", title).apply();
sp.edit().putString("notification-content", content).apply();
}
}
注:-
这是一个不完整的解决方案,需要大量额外的代码来处理所有场景。
其一,当设备重启时,AlarmManager 被清除,因此您必须处理此广播接收器中的 ACTION_BOOT_COMPLETED
、ACTION_LOCKED_BOOT_COMPLETED
、ACTION_REBOOT
和 ACTION_MY_PACKAGE_REPLACED
事件。
或按照此 link 使用 WorkManager
-> https://martian.ventures/mantra/insights/schedule-local-notifications-on-android/
如果发现任何语法错误,请更正。