2017-10-14 1 views

Antwort

0

Ja. Kann das tun. Schauen Sie sich die Referenz Firebase Notification Github Repo

Push-Benachrichtigung wird basierend auf zwei Dinge ausgelöst werden

  • an das jeweilige Gerät senden (basierend auf dem fcm Token)
  • schicken Gruppe von Geräte (bezogen auf das Thema abonniert
  • )

onMessageReceived(RemoteMessage remoteMessage) wird aufgerufen, wenn Nachricht

in dem Gerät empfangen kann den rem Zugriff ote Nachricht und führen Sie die Aktion auf der Grundlage der Bedarf und Anforderung

Ich versuche es einfach zu halten für die anfängliche Entwicklung

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // Do nothing if message is null 
    if (remoteMessage == null) return; 

    // Data exists 
    if (remoteMessage.getData().size() > 0) { 
     // To show the notification pass the entire remote message as string 
     sendNotification(remoteMessage.getData().toString()); 

     // if you've JSON string then decode and handle accordingly 
     // Sample 
     /** 
      try { 
       JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
       sendNotification(JSONObject json); // Need to add method to the class to handle this 
      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      }   

     */ 
    } 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     String channelId = getString(R.string.default_notification_channel_id); 
     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = 
       new NotificationCompat.Builder(this, channelId) 
       .setSmallIcon(R.drawable.ic_stat_ic_notification) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

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

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 
Verwandte Themen