ios - 本机脚本 | 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);
});
解决方案
ios 的注册令牌与 android 不同。我遇到了你遇到的同一堵墙。您需要https://github.com/node-apn/node-apn
用于 IOS 推送通知。并firebase
用于 android 通知。您可以通过保存令牌在后端执行逻辑。有一个名为type
which 的字段是ios or android
你使用 iosnode-apn
和 firebase 提供的 android 使用sendToDevice
。
这就是我目前正在使用的当前 nativescript 项目的内容,我正在处理涉及推送通知的内容。希望对你有帮助,伙计。
推荐阅读
- android - Android Realm 存储自定义对象数组
- flutter - 使用本地通知颤动处理 FCM 背景
- java - 调用 Microsoft Identity Platform 时访问令牌签名验证失败
- c - 来自 CS50 第 2 讲 / strln:printf 和 for 之间的链接
- python - python 在 bash 或 shell 脚本中是否有类似于“set -x -o verbose”的东西(查看所有代码执行)?
- spring-boot - 如何在 Spring Boot 中确定给定 URL 的控制器名称和方法
- php - Laravel:在 web.php 中传递变量
- makefile - 如何正确设置 makfile 来编译不同的相关文件?
- javascript - 为什么使用带有 knexjs 的 expressjs 会出现错误 304。出于这个原因,数据没有得到正确
- mongodb - 如何在多个选项之间使用或更新()