Android O 中未设置 Firebase Cloud Messaging 后台通知通道 ID
Firebase Cloud Messaging background notification channel ID not being set in Android O
Firebase 似乎没有将传入消息的通知 channelId 设置为 android M。我正在关注 this guide,并试图在我的应用程序处于背景.
这是我的应用程序代码和清单。
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,如从 Firebase 控制台发送的屏幕截图所示:
我也试过通过 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\"}}"
在这两种情况下,消息都到达了我的设备,但没有 channelId(如上面日志消息中的 channelId=null 所示)。
在前台,我可以通过服务捕获通知并手动在其上粘贴一个频道 ID(即使我必须在消息的有效负载中对频道 ID 进行编码)。但我也需要后台通知才能正常工作,据我所知,这取决于 Firebase 库。
Firebase Release Notes表示在版本 10.2.6(2017 年 5 月 17 日)中添加了对通知渠道的支持:
Added support for Android O notification channels. Android clients can
specify a default notification channel in the application manifest
which will be used if the downstream message does not contain a
notification_channel parameter.
更新您的构建以使用版本 10.2.6 或更高版本。
Firebase 似乎没有将传入消息的通知 channelId 设置为 android M。我正在关注 this guide,并试图在我的应用程序处于背景.
这是我的应用程序代码和清单。
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,如从 Firebase 控制台发送的屏幕截图所示:
我也试过通过 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\"}}"
在这两种情况下,消息都到达了我的设备,但没有 channelId(如上面日志消息中的 channelId=null 所示)。
在前台,我可以通过服务捕获通知并手动在其上粘贴一个频道 ID(即使我必须在消息的有效负载中对频道 ID 进行编码)。但我也需要后台通知才能正常工作,据我所知,这取决于 Firebase 库。
Firebase Release Notes表示在版本 10.2.6(2017 年 5 月 17 日)中添加了对通知渠道的支持:
Added support for Android O notification channels. Android clients can specify a default notification channel in the application manifest which will be used if the downstream message does not contain a notification_channel parameter.
更新您的构建以使用版本 10.2.6 或更高版本。