public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
int notifyID = 1;
String CHANNEL_ID = "Class Over";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "my_channel_name";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription("notification channel description");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(mChannel);
}
Calendar rightNow = Calendar.getInstance();
for(int i=0; i<classes.size(); i++)
{
if(rightNow.get(Calendar.HOUR_OF_DAY) == classes.get(i).getClassHour() && rightNow.get(Calendar.MINUTE) == classes.get(i).getClassMin())
{
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Any Homework for " + classes.get(i).getClassName())
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notifyID, notification.build());
}
}
}
答案 0 :(得分:0)
要在android中安排作业,您需要了解作业调度程序或 fireBaseJobDispatcher,您可能还需要broadCast接收器,具体取决于您希望加载通知的条件, 一些有用的链接:) https://developer.android.com/topic/performance/scheduling.html https://developer.android.com/reference/android/content/BroadcastReceiver.html
答案 1 :(得分:0)
请尝试以下代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null) {
remoteMessage.getNotification();
}
if (remoteMessage.getData() != null) {
Map<String, String> data = remoteMessage.getData();
String message = data.get("message");
generateNotification(this,message, false);
}
}
}
public void generateNotification(Context context, String message, boolean isSilent) {
// Message does not null or empty
if (CommonUtil.isNullString("" + message)) {
return;
}
NotificationManager mNotificationManager;
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
context.getResources(),
R.drawable.ic_img);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
context).setAutoCancel(true)
.setContentTitle("" + context.getResources().getString(R.string.app_name))
.setContentText(message)
.setColor(context.getResources().getColor(R.color.colorWhite))
.setSmallIcon(R.drawable.ic_img);
.setLargeIcon(notificationLargeIconBitmap);
Intent resultIntent = new Intent(context, ProfileNavigationActivity.class);
resultIntent.putExtra(Constants.INTENT_NOTIFICATION, Constants.INTENT_NOTIFICATION_VALUE);
resultIntent.putExtra(Constants.INTENT_NOTIFICATION_MESSAGE, "" + message);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(),
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setContentText("" + message);
Notification notification = mBuilder.build();
mBuilder.setPublicVersion(notification);
// If isSilent flag is true, does not make sound and vibrate.
if (!isSilent) {
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Notification id allows you to updateFragment the notification later on.
if (mNotificationManager != null) {
mNotificationManager.notify(0, notification);
}
}