当我意识到 Permissions.askAsync 无法按预期工作时,问题就开始了。
我发现 Permissions.askAsync 没有按预期工作,它对于 ios 来说是很酷的解决方案,但我需要它用于 android!因此,我为此添加了一些额外的代码:
Alert.alert(
'No Notification Permission',
'please go to settings and enable notifications permissions manually',
[
{ text: 'cancel', onPress: () => console.log('cancel') },
{
text: 'Allow',
onPress: async () => {
if (Platform.OS === 'android') {
await IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_APP_NOTIFICATION_SETTINGS,
{
data: `package:${Application.applicationId}`,
}
);
}
if (Platform.OS === 'ios') {
Linking.openURL('app-settings:');
}
},
},
],
{ cancelable: false },
);
UPD。下面的构造效果很好,但我想直接访问 APP_NOTIFICATION_SETTINGS。
onPress={() => {
IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_APPLICATION_DETAILS_SETTINGS,
{
data: `package:${Application.applicationId}`,
}
);
}}
expo论坛中的相关问题https://forums.expo.io/t/opening-device-settings-on-android-using-linking/2059/14
我尝试访问 APP_NOTIFICATION_SETTINGS 但由于某种原因我收到错误,如 “在已安装的应用程序列表中找不到该应用程序”。在已发布的项目和独立(apk)上尝试过它并得到相同的结果。有谁知道是什么问题吗?
我意识到这有点晚了,但建议的答案对我不起作用。这就是我使用 Android 版本 29 的设备上的效果:
const pkg = Constants.manifest.releaseChannel
? Constants.manifest.android.package
: 'host.exp.exponent';
IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_APP_NOTIFICATION_SETTINGS,
{
extra: { 'android.provider.extra.APP_PACKAGE': pkg }
},
);
TL;DR:此处的键更改为
android.provider.extra.APP_PACKAGE
作为 extra
键名称。
解决方案:
const pkg = Constants.manifest.releaseChannel
? Constants.manifest.android.package // When published, considered as using standalone build
: 'host.exp.exponent'; // In expo client mode
onPress: () => {
if (Platform.OS === 'android') {
if (Platform.Version >= 26) {
IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_APP_NOTIFICATION_SETTINGS,
{
data: `package:${pkg}`,
},
);
} else {
IntentLauncher.startActivityAsync(
IntentLauncher.ACTION_APPLICATION_DETAILS_SETTINGS,
{
data: `package:${pkg}`,
},
);
}
}
if (Platform.OS === 'ios') {
Linking.openURL('app-settings:');
}
},
解决方案说明:https://forums.expo.io/t/api-to-open-app-settings/5290/18
现代世博会:
import * as Application from 'expo-application';
import * as IntentLauncher from 'expo-intent-launcher';
if (Platform.OS === 'ios') {
Linking.openURL(`app-settings:`);
} else {
await IntentLauncher.startActivityAsync(
IntentLauncher.ActivityAction.APP_NOTIFICATION_SETTINGS,
{ extra: { 'android.provider.extra.APP_PACKAGE': Application.applicationId } }
)
}