Firebase似乎没有将传入消息的Notification 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库可以做到正确。
答案 0 :(得分:3)
Firebase Release Notes表示在版本10。2。6(2017年5月17日)中添加了对通知渠道的支持:
添加了对Android O通知渠道的支持。 Android客户端可以 在应用程序清单中指定默认通知通道 如果下游消息不包含a,将使用它 notification_channel参数。
更新您的版本以使用版本10.2.6或更高版本。