1

hereというチュートリアルを使って作成したFirebaseアプリケーションがあります。私はAndroidの初心者なので、これを解決するのを手伝ってください。私のアプリは唯一の活動をしています。Firebase通知をクリックしたときにメッセージが読み込まれない

Firebaseコンソールからメッセージを送信すると、アプリが開いているときにメッセージとトーストが画面に表示されます。 しかし、アプリが実行されていない場合、通知バーでFirebaseからの通知を見ることができますが、それをクリックすると、トーストが来ないか、メッセージが画面に表示されます。 すべての機能は、アプリが初めてインストールされたときにうまく機能します。 この問題を解決するにはどうすればよいですか?親切に助けてください。以下は、私のコードは次のとおりです。

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> 

メイン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インスタンス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メッセージング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"; 
} 

は親切に、この問題の解決に役立ちます。

答えて

1

onMessageReceivedメソッドは、通知タイプのメッセージを送信するときに、アプリケーションが開いているときにのみ呼び出されます。 onMessageReceivedを実行したい場合は、アプリケーションを閉じてから「データメッセージ」を送信してください。データメッセージの詳細については、hereをクリックしてください。サーバーからデータ通知を送信します。あなたがデータメッセージを送信するために言及したチュートリアルからhttps://stackoverflow.com/a/38795553/3529309

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

使用push.phpクラスから取得されたデータメッセージの JSONサンプル。

1

アプリがバックグラウンドのときに通知をクリックできるようにするには、通知ペイロードにclick_action属性が必要です。

Firebaseドキュメントのsectionを確認してください。

また、click_action属性を定義するときは、起動するアクティビティの<intent-filter>に対応する<action>属性も必要です。

このvideoは非常に詳細に説明しています。

Fireboxコンソールから通知を送信する場合は、click__action属性を設定できないことに注意してください。独自の管理サーバから通知を送信する場合、またはFirebase Cloud Functionsを使用する場合にのみ行うことができます。

最後に、起動されたアクティビティで、data属性(前述のリンク先と同じドキュメントにも表示されています)を使用して、さらにDataを設定することができます。通知をクリックしてアプリを起動すると、getIntent()を使用して通知データを取得できます。それを行う方法の詳細については、this answerを参照してください。

関連する問題