android - React Native Expo - 安卓推送通知
问题描述
我正在尝试使用 expo 推送通知工具向 android 设备发送推送通知,(它适用于 iOS)但它不起作用,我添加了一个带有单独的 android 应用程序 google-service.json 的 firebase 项目,并运行了 expo push:android:upload --api-key 。有什么遗漏吗?
我使用托管工作流 SDK 39
这是我使用过的代码,它适用于 iOS
获取博览会推送通知代码
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== 'granted') {
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
let uid;
firebase.auth().signInAnonymously()
.then(() => {
uid = firebase.auth().currentUser.uid;
})
.then(() => {
db.collection('users').doc(uid).get()
.then(function(doc) {
if (doc.exists) {
db.collection('users').doc(uid).update({
expoPushToken: token,
'last use date': new Date().toISOString().slice(0,10),
'region': Localization.region
})
} else {
db.collection('users').doc(uid).set({
expoPushToken: token,
'last use date': new Date().toISOString().slice(0,10),
'region': Localization.region
})
}
})
})
.catch(() => console.log('ERROR'))
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}
应用程序.json
"android": {
"useNextNotificationsApi": true,
"googleServicesFile": "./google-services.json",
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
},
"package": "com.NAME.NAME",
"versionCode": 3
},
解决方案
推荐阅读
- java - 对于使用 slf4j/logstash 的 Jboss 应用程序,如何以编程方式从 Java 中的特定类文件中查找特定日志消息?
- sql - SQL:如何创建具有时间条件的表?
- c# - 即使使用新的空位图,gdi+ 也会发生一般错误
- javascript - 使用 JavaScript 定位和更新表中的特定元素
- python - 如何使用 Python 从导入的 csv 计算纬度/经度点之间的距离?
- c++ - C ++:即使删除数组后,数组的大小也没有改变
- javascript - 无法在 hbs 中显示 MySql 记录
- java - 如何在静态类中使用@Value?
- unity3d - Unity URP 2D 灯光
- sql - 使用 SQL 查询获取相对于其中存储的值的列数