在主要活动中,我有一个像startService(new Intent(MainActivity.this, Service.class));
这样的服务开始了。我的服务是假定根据提供的数据在某些日期发送通知。所有这些都工作正常,只是它仅在用户打开应用程序以再次运行服务时才起作用。有没有一种方法可以使该服务可以从我的Firebase数据库接收数据以发送通知,而不必再次启动该服务。
这是我的服务
public class Service extends android.app.Service {
public DatabaseReference databaseReference, PantryReference, RefReference;
public ChildEventListener childEventListener, childEventListener1, childEventListener2;
public Date dateFormat;
public String mContentTitle , mContentText;
public Notification notification;
private boolean mRunning;
public NotificationCompat.Builder groupBuilder;
public int UNIQUE_ID;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Random random = new Random();
UNIQUE_ID = 10000 + random.nextInt(99999);
groupBuilder =
new NotificationCompat.Builder(getApplicationContext(), "Default")
.setSmallIcon(R.drawable.ic_restaurant_black_24dp)
.setContentTitle(mContentTitle)
.setGroupSummary(true)
.setGroup("GROUP_NOTIF")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hi"));
//----------------------------------------------------------------------- Since Service cannot get groupUid in static from MainActivity
SharedPreferences sharedPreferences = getSharedPreferences("GroupUid", Context.MODE_PRIVATE);
String mGroupUid = sharedPreferences.getString("GroupUid", groupUid);
//----------------------------------------------------------------------------------- Setting up Notification
databaseReference = FirebaseDatabase.getInstance().getReference().child("Notifications")
childEventListener = databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
HashMap<String, String> value = (HashMap<String, String>) dataSnapshot.getValue();
if (value != null) {
String name = value.get("name");
String date = value.get("date");
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy", Locale.US);
try {
dateFormat = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
//------------------------------------------ setting the dates
Calendar cal = Calendar.getInstance();
cal.setTime(dateFormat);
String TodayDate = sdf.format(new Date());
if (TodayDate.equals(itemsDate)){
mContentText = name;
mContentTitle = "NOTIF";
Random random = new Random();
int k = 10000 + random.nextInt(99999);
NotificationCreate(mContentTitle, mContentText, k);
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return super.onStartCommand(intent, flags, startId);
}
//------------------------------------------------------- Creating Method so I dont have to keep repeating
public void NotificationCreate(String Title, String Description, int uniqueID){
Intent openApp = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 100, openApp, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(getApplicationContext(), "Default")
.setSmallIcon(R.drawable.ic_restaurant_black_24dp)
.setContentTitle(Title)
.setContentText(Description)
.setLights(Color.GREEN, 2000, 1000)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setStyle(new NotificationCompat.BigTextStyle().bigText(Description))
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setGroup("EXPIRATION_GROUP_NOTIF")
.build();
createNotificationChannel(getApplicationContext());
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
notificationManager.notify(UNIQUE_ID, groupBuilder.build());
notificationManager.notify(uniqueID, notification);
}
//--------------------------------------------------------------Needed for higher APIs
private void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "ChannelName";
String description = "Description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("Default", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
}
//---------------------------------------- Firebase stuff
@Override
public void onDestroy() {
if (databaseReference != null && childEventListener != null) {
databaseReference.removeEventListener(childEventListener);
}
}
}