1

我正在使用Android O模拟器并希望从Firebase控制台获得通知。它在除Android O之外的每个设备上都正常工作。我在日志中收到此错误。Firebase通知不适用于Android O

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 

我知道我必须指定Channel 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...> 

我还指定通道ID在FirebaseMessagingService这样

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()); 
} 
} 

所以做这一切之后。我仍然在日志中收到该错误消息,无法接收来自控制台的通知。任何帮助都是可观的。

+0

背后的任何原因downvoting? –

+0

你是否看到'From:'日志消息,指示收到消息? –

+0

当应用程序在前台,然后我可以看到。但在应用程序转到后台后显示错误。@ BobSnyder –

回答

1

当FCM消息不包含数据有效内容,仅包含通知有效内容,并且您的应用位于后台时,Firebase将直接生成通知,并且不会调用onMessageReceived()。这在the documentation中有解释。

对于Android O,必须创建通知通道。您正在清单中正确定义默认通知通道,但通道是在onMessageReceived()代码中创建的,该代码在应用程序位于后台时不会执行。

移动创建通道的代码,您可以确定它会在收到通知之前执行该通道。

另外,如Firebase Release Notes所示,在版本10.2.6中添加了对通知通道的支持。您必须至少使用该版本进行构建。