我正在开发一个用于发出产品请求的应用。更像是一个购物应用程序。当管理员发布一个时,用户将获得优惠。
我使用了firebase并创建了一个名为offers
的根,并在商品节点下添加了商品。
我创建了一个firebasebackground侦听器服务并添加了onChildEventListener。当发布新的报价时,将通知用户该报价。
Firebasebackgroundservice.java:
public class FirebaseBackgroundService extends Service {
private DatabaseReference f;
NotificationManager manager;
Notification myNotication;
private TinyDB database;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
super.onCreate();
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
database=new TinyDB(getApplicationContext());
FirebaseApp.initializeApp(getApplicationContext());
f= FirebaseDatabase.getInstance().getReference();
final DatabaseReference foff=f.child("offers");
foff.limitToLast(1).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<String>offarray=new ArrayList<String>();
offarray=database.getListString("offdata");
if(!offarray.contains(dataSnapshot.getKey())){
offarray.add(dataSnapshot.getKey());
database.putListString("offdata",offarray);
Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
String title=newPost.get("title").toString();
String code=newPost.get("code").toString();
postNotif(title,code);
}
}
@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) {
}
});
//Order updates
String uname=database.getString("uname");
final DatabaseReference fupd=f.child("users").child(uname).child("notif");
fupd.limitToLast(1).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<String>offarray=new ArrayList<String>();
offarray=database.getListString("updata");
if(!offarray.contains(dataSnapshot.getKey())){
offarray.add(dataSnapshot.getKey());
database.putListString("updata",offarray);
Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
String title=newPost.get("title").toString();
String code=newPost.get("code").toString();
updateNotif(title,code);
}
}
@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 START_STICKY;
}
private void postNotif(String title,String descp) {
Context context=getApplicationContext();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
try {
int smallIcon = R.drawable.ic_card;
long when = System.currentTimeMillis();
CharSequence contentTitle = title;
CharSequence contentText = descp;
Intent notificationIntent = new Intent(context,OfferActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setWhen(when)
.setContentText(contentText)
.setContentTitle(contentTitle)
.setSmallIcon(smallIcon)
.setAutoCancel(true)
.setTicker(contentTitle)
.setLargeIcon(icon)
.setContentIntent(pendingIntent)
.setSound(alarmSound);
notificationManager.notify((int) when, notificationBuilder.build());
} catch (Exception e) {
Log.e("Notification Exception", e.getMessage());
}
}
private void updateNotif(String title,String descp) {
Context context=getApplicationContext();
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try {
int smallIcon = R.drawable.ic_file_upload;
long when = System.currentTimeMillis();
CharSequence contentTitle = title;
CharSequence contentText = descp;
Intent notificationIntent = new Intent(context,Notifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setWhen(when)
.setContentText(contentText)
.setContentTitle(contentTitle)
.setSmallIcon(smallIcon)
.setAutoCancel(true)
.setTicker(contentTitle)
.setLargeIcon(icon)
.setContentIntent(pendingIntent)
.setSound(alarmSound);
notificationManager.notify((int) when, notificationBuilder.build());
} catch (Exception e) {
Log.e("Notification Exception", e.getMessage());
}
}
@Override
public void onDestroy(){
super.onDestroy();
startService(new Intent(this,FirebaseBackgroundService.class));
}
}
该应用程序工作正常,但我在这里遇到两个问题:
1.最初启动服务时,会显示通知。但有时服务器的服务是killed
。重新启动服务时,不会显示通知。该服务正在被杀死,并且很长时间没有重新启动。
2.服务childAddedListener
后未调用resumes
,因此通知也未显示
该应用程序在首次启动服务时运行良好。一旦服务被终止,通知就不会显示,childaddedlistener
无法正常工作
请帮我解决这个问题。我经常搜索firebasebackgroundservice
,但结果却是这样。谢谢。