【问题标题】:Why Firebase Function calling Firebase admin.messaging.sendToDevice() cannot create notification on Android device为什么调用 Firebase admin.messaging.sendToDevice() 的 Firebase 函数无法在 Android 设备上创建通知
【发布时间】: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


    【解决方案1】:

    要让应用在前台或后台运行通知,您必须在您的 Android 应用中实现并注册 FirebaseMessagingService,以接收 onMessageReceived() 上的调用,然后在收到通知消息时手动在其中使用 NotificationManager 创建通知进来。

    来自the documentation

    Firebase 通知的行为因接收应用的前台/后台状态而异。如果您希望前台应用接收通知消息或数据消息,则需要编写代码来处理 onMessageReceived 回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多