【发布时间】:2019-05-13 01:39:01
【问题描述】:
我有一个 Android 应用,并尝试使用 Firebase 函数中的 Firebase 消息传递 API 向安装它的特定设备发送通知。但是通知永远不会出现。但是,从 Firebase 控制台手动发送确实会在同一设备上成功显示通知。以下是相关的 Firebase 函数代码:
var message = {
notification: {
title: notifTitle,
body: notifBody,
sound: 'default',
android_channel_id: 'update_channel',
channel_id: 'update_channel'
// tag:
}
};
var options = {}
console.log(message);
console.log('registrationToken: ' + registrationToken);
// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().sendToDevice([registrationToken], message, options)
.then((response) => {
console.log(response);
...
调用时会记录以下内容,似乎已成功发送通知:
{ results: [ { messageId: '0:1544572985549904%5be491645be49164' } ],
canonicalRegistrationTokenCount: 0,
failureCount: 0,
successCount: 1,
multicastId: 5825519571250606000 }
在我的 Android 应用中,我在主要活动的 onCreate() 中创建了 update_channel(因为根据我收集到的信息,Android Oreo API 26+ 需要一个特定的通道...):
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String id = getString(R.string.update_notification_channel);
CharSequence name = getString(R.string.app_name);
String description = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(id, name, importance);
channel.setImportance(NotificationManager.IMPORTANCE_HIGH);
//channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
并将其设置为 Manifest 中的默认频道:
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="update_notification_channel"/>
我不知道问题是否与渠道有关,或者我是否在某处遗漏了一步,或者是什么。
【问题讨论】:
标签: android firebase firebase-cloud-messaging google-cloud-functions android-notifications