服务中的android-firebase childEventListener [英] android-firebase childEventListener in service

查看:34
本文介绍了服务中的android-firebase childEventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用程序中,即使该应用程序未激活,我仍在尝试实现firebase数据库的childEventListener.表示,只要firebase db特定子项具有新条目,它都应通知用户.所以直到现在我都做完了,

in my android application i am trying to implement childEventListener of firebase database even when the app is not active. means whenever the firebase db particular child has a new entry it should notify the user.. so till now i have done with this,

我已在清单文件中声明服务为

i have declared service in manifest file as

<service android:name=".ChildEventListener"/>

然后我的ChildEventListener类如下

then my ChildEventListener class is as follows

public class ChildEventListener extends Service {

FirebaseAuth auth;
NotificationCompat.Builder builder;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent,  int flags, int startId) {
    retrivemsg();
    return START_STICKY;
}

public void retrivemsg()
{
    DatabaseReference mdb= FirebaseDatabase.getInstance().getReference("messages/"+auth.getCurrentUser().getUid());
    builder=new NotificationCompat.Builder(this,"onchildadded");
    mdb.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            String msg=dataSnapshot.child("msg").getKey();
            builder.setSmallIcon(R.drawable.send);
            builder.setContentTitle("child added");
            builder.setContentText(msg);
            NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(1,builder.build());
        }

        @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) {

        }
    });
}

}

但是我尝试了,但是没有运气...即使添加了新孩子,我也没有收到任何通知....即使应用程序处于前台状态也没有...

but i tried, but no luck... even the new child is added, i am not getting any notification.... not even when the app is in foreground...

推荐答案

实际上,我们可以在清单文件中启动服务.但是在我的情况下,我的错误是我没有在我真正想要启动它的地方添加启动服务语句.因此只需添加

actually we can start a service in manifest fie. but in my case my mistake was i didnt added start service statement where i actually want to start it. so just by adding

startService(new Intent(this, ChildEventListener.class));

我是一个我想开始的班级.解决了我的问题.

i a class where i want to start it. solved my problem.

-Thanx @ merterpam,指出了我的错误

-Thanx @ merterpam, for pointing out my mistake

这篇关于服务中的android-firebase childEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆