2017-06-29 7 views
0

ich dort bin mit Feuerbasis kann ich sehen, wenn ein neues „Kind“ hinzugefügt wird, ich eine Benachrichtigung zeigen müssen, wenn ein neues Kind hinzugefügt wird ich diesen Code verwende, aber es funktioniert nichtWie zeigen Sie Benachrichtigungen an, wenn der Benutzer nicht mehr in der App ist?

(Die Benachrichtigungen arbeiten zeigt aber nur in einer Aktivität, muss ich die Benachrichtigung angezeigt, wenn der Nutzer aus der App ist, wie Facebook oder whatsapp

Methode:

public Notification createNotification(boolean makeHeadsUpNotification) { 
    Notification.Builder notificationBuilder = new Notification.Builder(this) 
      .setSmallIcon(R.drawable.ic_settings_real_black_24dp) 
      .setPriority(Notification.PRIORITY_DEFAULT) 
      .setContentTitle("Sample Notification") 
      .setContentText("This is a normal notification."); 


     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      notificationBuilder.setCategory(Notification.CATEGORY_MESSAGE); 
     } 

    if (makeHeadsUpNotification) { 
     Intent push = new Intent(); 
     push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     push.setClass(this, UserActivity.class); 

     PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, 
       push, PendingIntent.FLAG_CANCEL_CURRENT); 
     notificationBuilder 
       .setContentText("Heads-Up Notification on Android L or above.") 
       .setFullScreenIntent(fullScreenPendingIntent, true); 
    } 
    return notificationBuilder.build(); 
} 

// Methode für Benachrichtigungen, wenn Kind hinzugefügt wird

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 


    // THIS METHODS ARE IN THE ONCREATE 

    mListener = FirebaseUtils.getCHATSOLICITUDRef("8HaKY67AjaOD2a6QKrABhR2rcaJ3").addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 


      /// THE METHOD IS IN THIS 
      mNotificationManager.notify(1, createNotification(
        true)); 
     } 

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

     } 
    }); 




} 

Antwort

1

können Sie mit dem Services so wird es im Hintergrund arbeiten auch meinen Code here

+0

Wie Sie es implementieren überprüfen? Ich erstelle die Klasse und lege sie in das Manifest und das war's? und das zeigt, wenn Benutzer die App aus? @Oussema Aroua –

+0

Ja einfach so –

+0

Wie stellst du das Manifest ein? –

Verwandte Themen