1

Ich habe eine Firebase-Anwendung mit dem gegebenen Lernprogramm here erstellt. Da ich ein Anfänger für Android bin, bitte hilf mir das zu lösen. Meine App hat nur eine Aktivität.Nachrichten werden beim Klicken auf die Firebase-Benachrichtigung nicht geladen

Wenn ich eine Nachricht von meiner Firebase-Konsole aus sende, kann ich die Nachricht und den Toast auf dem Bildschirm sehen, wenn die App geöffnet ist. Wenn die App jedoch nicht ausgeführt wird, kann ich die Benachrichtigung von Firebase in der Benachrichtigungsleiste sehen, aber beim Klicken wird weder der Toast angezeigt noch die Nachricht auf dem Bildschirm angezeigt. Alles funktioniert nur zum allerersten Mal, wenn die App installiert wird. Wie kann ich dieses Problem lösen? Freundlich helfen. Hier sind meine Codes:

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.unss.pramod.firebasetest.activity.MainActivity"> 

    <TextView 
     android:id="@+id/txt_push_message" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="40dp" 
     android:gravity="center_horizontal" 
     android:textColor="@color/colorPrimary" 
     android:textSize="26dp" /> 

    <TextView 
     android:id="@+id/txt_reg_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" /> 
</RelativeLayout> 

Haupt Activity.java

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private BroadcastReceiver mRegistrationBroadcastReceiver; 
    private TextView txtRegId, txtMessage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     txtRegId = (TextView) findViewById(R.id.txt_reg_id); 
     txtMessage = (TextView) findViewById(R.id.txt_push_message); 

     mRegistrationBroadcastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 

       // checking for type intent filter 
       if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) { 
        // gcm successfully registered 
        // now subscribe to `global` topic to receive app wide notifications 
        FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL); 

        displayFirebaseRegId(); 

       } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) { 
        // new push notification is received 

        String message = intent.getStringExtra("message"); 

        Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show(); 

        txtMessage.setText(message); 
       } 
      } 
     }; 

     displayFirebaseRegId(); 
    } 

    // Fetches reg id from shared preferences 
    // and displays on the screen 
    private void displayFirebaseRegId() { 
     SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0); 
     String regId = pref.getString("regId", null); 

     Log.e(TAG, "Firebase reg id: " + regId); 

     if (!TextUtils.isEmpty(regId)) 
      txtRegId.setText("Firebase Reg Id: " + regId); 
     else 
      txtRegId.setText("Firebase Reg Id is not received yet!"); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // register GCM registration complete receiver 
     LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
       new IntentFilter(Config.REGISTRATION_COMPLETE)); 

     // register new push message receiver 
     // by doing this, the activity will be notified each time a new message arrives 
     LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, 
       new IntentFilter(Config.PUSH_NOTIFICATION)); 

     // clear the notification area when the app is opened 
     NotificationUtils.clearNotifications(getApplicationContext()); 
    } 

    @Override 
    protected void onPause() { 
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver); 
     super.onPause(); 
    } 
} 

Firebase-Instanz-ID service.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 
    private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName(); 

    @Override 
    public void onTokenRefresh() { 
     super.onTokenRefresh(); 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 

     // Saving reg id to shared preferences 
     storeRegIdInPref(refreshedToken); 

     // sending reg id to your server 
     sendRegistrationToServer(refreshedToken); 

     // Notify UI that registration has completed, so the progress indicator can be hidden. 
     Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE); 
     registrationComplete.putExtra("token", refreshedToken); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
    } 

    private void sendRegistrationToServer(final String token) { 
     // sending gcm token to server 
     Log.e(TAG, "sendRegistrationToServer: " + token); 
    } 

    private void storeRegIdInPref(String token) { 
     SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0); 
     SharedPreferences.Editor editor = pref.edit(); 
     editor.putString("regId", token); 
     editor.commit(); 
    } 
} 

Firebase Messaging service.java

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); 

    private NotificationUtils notificationUtils; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 

     if (remoteMessage == null) 
      return; 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
      handleNotification(remoteMessage.getNotification().getBody()); 
     } 

     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString()); 

      try { 
       JSONObject json = new JSONObject(remoteMessage.getData().toString()); 
       handleDataMessage(json); 
      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      } 
     } 
    } 

    private void handleNotification(String message) { 
     if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
      // app is in foreground, broadcast the push message 
      Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
      pushNotification.putExtra("message", message); 
      LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

      // play notification sound 
      NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
      notificationUtils.playNotificationSound(); 
     }else{ 
      // If the app is in background, firebase itself handles the notification 
     } 
    } 

    private void handleDataMessage(JSONObject json) { 
     Log.e(TAG, "push json: " + json.toString()); 

     try { 
      JSONObject data = json.getJSONObject("data"); 

      String title = data.getString("title"); 
      String message = data.getString("message"); 
      boolean isBackground = data.getBoolean("is_background"); 
      String imageUrl = data.getString("image"); 
      String timestamp = data.getString("timestamp"); 
      JSONObject payload = data.getJSONObject("payload"); 

      Log.e(TAG, "title: " + title); 
      Log.e(TAG, "message: " + message); 
      Log.e(TAG, "isBackground: " + isBackground); 
      Log.e(TAG, "payload: " + payload.toString()); 
      Log.e(TAG, "imageUrl: " + imageUrl); 
      Log.e(TAG, "timestamp: " + timestamp); 


      if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
       // app is in foreground, broadcast the push message 
       Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
       pushNotification.putExtra("message", message); 
       LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

       // play notification sound 
       NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
       notificationUtils.playNotificationSound(); 
      } else { 
       // app is in background, show the notification in notification tray 
       Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class); 
       resultIntent.putExtra("message", message); 

       // check for image attachment 
       if (TextUtils.isEmpty(imageUrl)) { 
        showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent); 
       } else { 
        // image is present, show notification with image 
        showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl); 
       } 
      } 
     } catch (JSONException e) { 
      Log.e(TAG, "Json Exception: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
    } 

    /** 
    * Showing notification with text only 
    */ 
    private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { 
     notificationUtils = new NotificationUtils(context); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     notificationUtils.showNotificationMessage(title, message, timeStamp, intent); 
    } 

    /** 
    * Showing notification with text and image 
    */ 
    private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { 
     notificationUtils = new NotificationUtils(context); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl); 
    } 
} 

Config.java

public class Config { 

    // global topic to receive app wide push notifications 
    public static final String TOPIC_GLOBAL = "global"; 

    // broadcast receiver intent filters 
    public static final String REGISTRATION_COMPLETE = "registrationComplete"; 
    public static final String PUSH_NOTIFICATION = "pushNotification"; 

    // id to handle the notification in the notification tray 
    public static final int NOTIFICATION_ID = 100; 
    public static final int NOTIFICATION_ID_BIG_IMAGE = 101; 

    public static final String SHARED_PREF = "ah_firebase"; 
} 

bitte helfen, dieses Problem zu lösen.

Antwort

1

Die Methode onMessageReceived wird nur aufgerufen, wenn Ihre Anwendung geöffnet/Vordergrund ist, wenn Sie "Benachrichtigungstyp Nachricht" senden. Wenn Sie onMessageReceived entweder Ihre Anwendung in der Nähe ausführen möchten, senden Sie "Datennachricht". Klicken Sie auf here, um mehr über Datennachrichten zu erfahren. Senden Sie Datenbenachrichtigungen vom Server. JSON Beispielnachricht für Daten entnommen aus https://stackoverflow.com/a/38795553/3529309

{ 
    "to": "/topics/dev_journal", 
    "data": { 
     "text":"text", 
     "title":"", 
     "line1":"Journal", 
     "line2":"test" 
    } 
} 

Verwenden push.php Klasse von Tutorial Sie erwähnten Datennachricht zu senden.

1

Damit Benachrichtigungen anklickbar sind, wenn sich Ihre App im Hintergrund befindet, benötigen Sie das Attribut click_action in Ihrer Benachrichtigungsnutzlast.

Bitte überprüfen Sie diese section der Firebase-Dokumente. Wenn Sie das Attribut click_action definieren, benötigen Sie außerdem ein entsprechendes <action>-Attribut in der <intent-filter> der Aktivität, die Sie starten möchten

Diese video erklärt es in ziemlich detaillierter Weise.

Beachten Sie jedoch, dass Sie das Attribut click__action nicht festlegen können, wenn Sie Benachrichtigungen von der Firebase-Konsole senden. Sie können dies nur tun, wenn Sie eine Benachrichtigung von Ihrem eigenen Admin-Server senden oder Firebase Cloud-Funktionen verwenden.

Schließlich können Sie in der Aktivität, die gestartet wird, zusätzliche Data mithilfe des Datenattributs festlegen (das auch in dem Dokument angezeigt wird, das ich oben verlinkt habe).Wenn Sie Ihre App starten, indem Sie auf eine Benachrichtigung klicken, können Sie die Benachrichtigungsdaten mit getIntent() abrufen. Weitere Informationen dazu finden Sie unter this answer.

Verwandte Themen