添加服务到我的Android应用程序,以便在添加fire base子项时收到通知

时间:2016-10-16 18:13:51

标签: android firebase android-service firebase-realtime-database firebase-cloud-messaging

我正在尝试创建一个Android应用程序,每当firebase子项添加到我的JSON时会出现通知,我只能在我打开应用程序或最近关闭它时收到通知。我有一些问题。 1)每当新安装时,我都会收到之前添加的所有子项的通知。我只想要新添加的儿童通知。 2)即使应用程序没有运行,即使在后台,我也想收到通知。有人可以帮我为此创建一个服务,这样我就可以开始工作了。

如果我做的事情没有意义,也请帮我解决下面的代码。

package com.fayaz.firebasenotify;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("user");

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
int notify=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent notificationIntent = new Intent(this, MainActivity.class);

    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    // Read from the database
    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value = dataSnapshot.getValue(String.class);
            String name = dataSnapshot.getKey();
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle(name);
            builder.setContentText(value);
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(value));
            builder.setContentIntent(contentIntent);
            builder.setSound(alarmSound);
            builder.setAutoCancel(true);
            notify=generateRandom();
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(notify, builder.build());
        }

        public int generateRandom(){
            Random random = new Random();
            return random.nextInt(9999 - 1000) + 1000;
        }

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

        }
    });
}
}

0 个答案:

没有答案