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。這從我的設備屏幕截圖確認信道被註冊,以及:
這裏的關鍵部分。 當我定位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,在這個截圖中描繪從火力地堡控制檯發送:
我有也嘗試通過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庫的正確性取決於此。
你正在搭建什麼版本的'firebase-messaging'?在使用11.4.2構建的我的測試應用程序中,當收到未註冊頻道ID的通知時,我會看到通知和此日誌消息:「W/FirebaseMessaging:Notification Channel requested(channel_X)尚未由應用程序創建。將使用清單配置或默認值。' –
嗯,這可能是問題。我只是使用firebase助手來安裝firebase-messaging包,它會自動選擇10.0.1。讓我更新並重試。 –