Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序启动时发送通知_Android_Firebase_Notifications_Firebase Realtime Database_Android Notifications - Fatal编程技术网

Android 应用程序启动时发送通知

Android 应用程序启动时发送通知,android,firebase,notifications,firebase-realtime-database,android-notifications,Android,Firebase,Notifications,Firebase Realtime Database,Android Notifications,我在childEventListener中实现了一个通知生成器,以便我的用户在新帖子可用时接收通知。。。通知生成器方法是: public void showNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.app_log

我在childEventListener中实现了一个通知生成器,以便我的用户在新帖子可用时接收通知。。。通知生成器方法是:

public void showNotification() {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.app_logo)
                        .setContentTitle("new post")
                        .setContentText("Check it out guys");
        Intent resultIntent = new Intent(this, MainActivity.class);
        android.support.v4.app.TaskStackBuilder stackBuilder = android.support.v4.app.TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int mId;
        mId = 1;
        mNotificationManager.notify(mId, mBuilder.build());

    }
在onCreate()方法中

mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts"); 
mDatabasePosts.addChildEventListener(new ChildEventListener() {


            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                showNotification();
            }

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

            }
        });
我的用户在将孩子添加到数据库时会收到通知,但在加载应用程序时也会收到通知。。。我尝试在onPause()上实现这一点,但每次他们按下home按钮时都会显示通知。请告诉我哪里出错了


谢谢

默认情况下,不管方法的名称如何,当您获取DatabaseReference时,firebase将获取所需子级的所有数据并返回。您需要做的是:

  • 将haveNewItems设置为true;在你们班
  • 将OnChildeded方法更改为:

    @覆盖
    公共void onChildAdded(DataSnapshot DataSnapshot,字符串s){
    如果(!haveNewItems)
    showNotification();
    }

  • 添加一个SingleValueListener,完成后,将haveNewItems设置为false


  • js中的方法可以理解

    我已经设法弄明白了。万一你们中的一些人也被卡住了,这是一个很好的方法

    因此,在您的活动中,创建一个类

    public static boolean isBackgroundRunning(Context context) {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            //If your app is the process in foreground, then it's not in running in background
                            return false;
                        }
                    }
                }
            }
    
    
            return true;
        }
    

    瞧,只有当你的应用程序在后台时,你才会收到通知。

    你在这个电话中收到旧的post通知吗?嗨@JdPrajapati嗯,事实上我不知道。。。但在创建应用程序时会调用通知。。。以及在数据库中添加新的子项时。。。但不是onResume();有什么方法可以帮助我确定是否有新孩子?谢谢。你也将使用OnChildaded。但是,只有在singleValueListener上实现的完成侦听器中将haveNewItems设置为false时,它才会显示您的通知
    mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts"); 
    mDatabasePosts.addChildEventListener(new ChildEventListener() {
    
    
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    if(isBackgroundRunning(MainActivity.this)){
    
                    showNotification();
                    }
                }
    
                @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) {
    
                }
            });