1

Android Oエミュレータを使用していて、Firebaseコンソールから通知を受けたい場合は、Android Oを除くすべてのデバイスで正常に動作しています。このエラーがログに記録されます。AndroidベースでFirebase通知が機能しない

W/Notification: Use of stream types is deprecated for operations other than volume control 
W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case 

私はチャネルIDを指定する必要があることを知っています。 は、私がこれまで

のAndroidManifest.xml

 <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:largeHeap="true" 
      android:roundIcon="@mipmap/ic_launcher_round" 
      android:supportsRtl="true" 
      android:theme="@style/AppTheme"> 

      <meta-data 
       android:name="com.google.firebase.messaging.default_notification_icon" 
       android:resource="@drawable/attach" /> 


      <meta-data 
       android:name="com.google.firebase.messaging.default_notification_color" 
       android:resource="@color/colorAccent" /> 

      <meta-data 
       android:name="com.google.firebase.messaging.default_notification_channel_id" 
       android:value="my_notification_channel" /> 

      <service android:name=".Helper.FirebaseIDService"> 
       <intent-filter> 
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
       </intent-filter> 
      </service> 

      <service android:name=".Helper.MyFirebaseMessagingService"> 
       <intent-filter> 
        <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
       </intent-filter> 
      </service> 

     <my all activities...> 

を行っているので、私もそう、すべてこれをやった後に、この

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    private static final String TAG = "FCM Service"; 
    private static final int NOTIFICATION_ID = 1; 
    private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
      NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT); 

      // Configure the notification channel. 
      notificationChannel.setDescription("Channel description"); 
      notificationChannel.enableLights(true); 
      notificationChannel.setLightColor(Color.RED); 
      notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); 
      notificationChannel.enableVibration(true); 
      notificationManager.createNotificationChannel(notificationChannel); 
     } 


     NotificationCompat.Builder mBuilder; 
     mBuilder = new NotificationCompat.Builder(getApplicationContext()) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Title") 
       .setContentText(remoteMessage.getNotification().getBody()) 
       .setOngoing(true) 
       .setChannelId(NOTIFICATION_CHANNEL_ID); 

     notificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 
} 

ようでFirebaseMessagingServiceをチャネルIDを指定しました。私はまだログにそのエラーメッセージが表示され、console.Anyヘルプからの通知を受け取ることができません。

+0

何らかの理由でdownvotingされていますか? –

+0

メッセージが受信されたことを示す 'From:'ログメッセージが表示されますか? –

+0

アプリがフォアグラウンドにあるとき、私はそれを見ることができます。アプリがバックグラウンドに移行した後、エラー@ BobSnyder –

答えて

1

FCMメッセージにデータペイロード、通知ペイロードのみが含まれていて、アプリがバックグラウンドにある場合、Firebaseは通知を直接生成し、onMessageReceived()はコールしません。これはで説明されています。

Android Oの場合、通知チャネルを作成する必要があります。マニフェストでデフォルトの通知チャネルを正しく定義していますが、チャネルがonMessageReceived()コードで作成されています。このコードは、アプリがバックグラウンドのときには実行されません。

チャネルを作成するコードを、通知を受信する前に実行することができる特定の場所に移動します。

また、Firebase Release Notesに示されているように、通知チャネルのサポートがバージョン10.2.6で追加されました。あなたは少なくともそのバージョンでビルドする必要があります。

関連する問題