首页 > 解决方案 > 本机脚本 | Firebase 通知不起作用

问题描述

我在我的 Nativescript 应用程序中使用 firebase 时遇到问题,当我使用 android 时它工作得很好但不能与 IOS 一起使用。问题出在消息发送上。我在客户端使用推送插件

这是IOS客户端使用pushPlugin的注册部分

const iosSettings:any = {
        badge: true,
        sound: true,
        alert: true,
        interactiveSettings: {
            actions: [{
                identifier: 'READ_IDENTIFIER',
                title: 'Read',
                activationMode: "foreground",
                destructive: false,
                authenticationRequired: true
            }, {
                identifier: 'CANCEL_IDENTIFIER',
                title: 'Cancel',
                activationMode: "foreground",
                destructive: true,
                authenticationRequired: true
            }],
            categories: [{
                identifier: 'READ_CATEGORY',
                actionsForDefaultContext: ['READ_IDENTIFIER', 'CANCEL_IDENTIFIER'],
                actionsForMinimalContext: ['READ_IDENTIFIER', 'CANCEL_IDENTIFIER']
            }]
        },
        notificationCallbackIOS: (message: any) => {
            alert(JSON.stringify(message));
        }
    };


    pushPlugin.register(iosSettings, (token: string) => {
        // update the token in the server
        alert("Device registered. Access token: " + token);
            });
        }
    }, (errorMessage: any) => {
        alert("Device NOT registered! " + JSON.stringify(errorMessage));
    });

这就是我收到推送通知的令牌的方式,在我使用推送应用程序时获得令牌后一切正常,我在 IOS 设备中收到通知,但问题是当我尝试发送通知时从服务器!

我收到此错误:

提供的注册令牌无效。确保它与客户端应用从 FCM 注册时收到的注册令牌匹配。

我的服务器中的节点代码

        var payload = {
        data: {
          targetId:userToken,
          body: "some text"
        }
      };
    var options = {
        priority: "high",
        contentAvailable: true,
        timeToLive: 60 * 60 * 24
      };
    Admin.messaging().sendToDevice(userToken, <any>payload,options)
        .then((response) => {
            console.log('notification arrived successfully', response.results[0]);
        })
        .catch((error) => {
            console.log('notification failed', error);
        });

标签: iosnode.jsfirebasefirebase-cloud-messagingnativescript

解决方案


ios 的注册令牌与 android 不同。我遇到了你遇到的同一堵墙。您需要https://github.com/node-apn/node-apn用于 IOS 推送通知。并firebase用于 android 通知。您可以通过保存令牌在后端执行逻辑。有一个名为typewhich 的字段是ios or android你使用 iosnode-apn和 firebase 提供的 android 使用sendToDevice

这就是我目前正在使用的当前 nativescript 项目的内容,我正在处理涉及推送通知的内容。希望对你有帮助,伙计。


推荐阅读