0

Firebase似乎沒有在傳入消息中設置通知渠道ID給Android M.我正在關注this guide,並試圖在我的應用程序獲取通知時觸發在後臺。未在Android中設置Firebase雲消息傳遞背景通知渠道ID O

這是我的應用程序代碼和清單。

public class MainActivity extends AppCompatActivity { 

    private void registerNotificationChannel(String id, String name, String description) { 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     int importance = NotificationManager.IMPORTANCE_HIGH; 
     NotificationChannel mChannel = new NotificationChannel(id, name, importance); 
     mChannel.setDescription(description); 
     mChannel.enableLights(true); 
     mChannel.setLightColor(Color.RED); 
     mChannel.enableVibration(true); 
     mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 
     mNotificationManager.createNotificationChannel(mChannel); 
    } 

    //private BroadcastReceiver isms; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG,"MainActivity.oncreate. token is:" + FirebaseInstanceId.getInstance().getToken()); 

     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     registerNotificationChannel("default","default","all other notifications"); 
     registerNotificationChannel("channel1","channel1","notification channel 1"); 
     registerNotificationChannel("channel2","channel2","notification channel 2"); 
    } 

    @Override protected void onDestroy() { 
     super.onDestroy(); 
    } 

} 

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.d(TAG, "firebase message received"); 
     if (remoteMessage.getNotification() != null) { 
      Map<String,String> data = remoteMessage.getData(); 
      for (Map.Entry<String, String> entry : data.entrySet()) 
      { 
       Log.d(TAG, "data: " + entry.getKey() + "/" + entry.getValue()); 
      } 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 

    } 
} 

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 
    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.d(TAG, "Refreshed firebase " + 
       "" + 
       "" + 
       "token: " + refreshedToken); 
    } 
} 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="org.phauna.alerter"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

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

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

    </application> 

</manifest> 

正如你所看到的,我肯定我的註冊通知渠道,因爲它說here。這從我的設備屏幕截圖確認信道被註冊,以及:

enter image description here

這裏的關鍵部分。 當我定位API版本25時,我收到後臺通知。只要我瞄準版本26,我就不會。相反, 我在logcat中收到此錯誤信息:

10-11 12:40:00.925 899 8910 E NotificationService: No Channel found for pkg=org.phauna.alerter, channelId=null, id=0, tag=GCM-Notification:286245598, opPkg=org.phauna.alerter, callingUid=10179, userId=0, incomingUserId=0, notificationUid=10179, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE) 

我肯定設置頻道ID,在這個截圖中描繪從火力地堡控制檯發送:

enter image description here

我有也嘗試通過HTTP服務發送:

curl -X POST --header "Authorization: key=<redacted>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<redacted>\",\"data\":{\"android_channel_id\":\"channel1\"},\"notification\":{\"body\":\"this is a testfoobar\"}}" 

在這兩種情況下,消息都會到達我的設備b ut沒有channelId(如上述日誌消息中的channelId = null所示)。

在前臺,我可以用服務捕獲通知並手動粘貼一個通道ID(即使我必須在消息的有效負載中對​​通道ID進行編碼)。但是我需要後臺通知才能正常工作,並且據我所知,Firebase庫的正確性取決於此。

+1

你正在搭建什麼版本的'firebase-messaging'?在使用11.4.2構建的我的測試應用程序中,當收到未註冊頻道ID的通知時,我會看到通知和此日誌消息:「W/FirebaseMessaging:Notification Channel requested(channel_X)尚未由應用程序創建。將使用清單配置或默認值。' –

+0

嗯,這可能是問題。我只是使用firebase助手來安裝firebase-messaging包,它會自動選擇10.0.1。讓我更新並重試。 –

回答

2

Firebase Release Notes的表明,在10.2.6版溶液中加入對通知信道支持(2017年5月17日):

爲Androidö通知信道增加的支持。 Android客戶端 可在應用程序清單 中指定默認通知通道,如果下游消息不包含 notification_channel參數,則將使用該通知通道。

更新您的版本以使用版本10.2.6或更高版本。

+0

我更新到11.4.2,我的問題已修復!感謝Bob! –

+0

我無法找到* android_channel_id ** @Bob你知道它在哪裏。 –

+0

@MaheshGawhane:不確定你的意思。可能是默認頻道[此處介紹](https://firebase.google.com/docs/cloud-messaging/android/client#manifest)。 –

相關問題