2017-09-20 2 views
0

Ich brauche Hilfe. Wie ich im Titel geschrieben habe, weiß ich nicht, wie man Daten von der Benachrichtigung hält, wenn sie ankommt und die Anwendung im Hintergrund ist oder getötet wird. Was ich übergeben muss (Code unten) ist eine Platte, die ich für eine Ausarbeitung brauche. Hier ist der Code:Wie Sie Daten von Benachrichtigungen behalten, während Android-Anwendung im Hintergrund oder geschlossen ist

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    String text= remoteMessage.getNotification().getBody(); 
    SharedPreferences s = getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE); 
    Functions.putStringInPrefs(s, Constants.PREFS_ALERT_PLATE, text); 


    if (Functions.isUserSignedUp(this)) { 

     if (remoteMessage.getNotification() != null) { 
      // Log.d(TAG, "Message Title:" + remoteMessage.getNotification().getTitle()); 

      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
      try { 
       sendNotification(remoteMessage.getNotification()); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


     } 


    } 
} 

private void sendNotification(RemoteMessage.Notification notification) { 
    Intent intent = new Intent(this, FragmentChangeActivity.class); 
    String prova = notification.getBody(); 



    intent.putExtra("alert_plate" , prova); 
    GlobalData.alertPlate=prova; // object used as cache 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    if(notification.getTitle()!= null){ 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.details_icon).setContentTitle(notification.getTitle()); 

    } else{ 
     new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.details_icon).setContentTitle(getString(R.string.app_name)); 
    } 

    new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.details_icon).setContentText(notification.getBody()) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(new Random().nextInt((100000000 - 1) + 1) + 1, new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.details_icon).build()); 



    } 


} 

Was muss ich ändern? Vielen Dank ! Ps. Im Manifest wird die Klasse korrekt deklariert.

Antwort

0

Es gibt zwei Arten von FCM-Meldungen

1) Mitteilung Nachricht 2) Daten Nachricht

Also, wenn Sie Daten empfangen möchten, wenn die App der Nähe haben Sie Ihre Daten mit Datennachricht zu senden. Ihr Datenformat wird etwas aussehen wie unten

{ 
    "to": "device_id", 
    "data": { 
     "param_1": "vale 1", 
     "param_2": "value 2", 
     "param_3": "Value 3" 
    } 
} 

Nach dem Empfang der Nachricht sein, jetzt Sie Ihre Daten in gemeinsamen Vorlieben oder Datenbank speichern können, was Sie wollen.

Verwandte Themen