英文:
Bring React Native app Background to Foreground
问题
使用react-native-push-notification
库的PushNotification
。当我们收到新通知并且应用程序在后台时,需要将应用程序带回前台,无需任何交互。我们使用了Linking.openURL('my-awesome-app://')
,但这只在应用程序处于前台时起作用,而在后台时不起作用。
localNotification(info) {
PushNotification.localNotification({
channelId: 'reminders',
title: '',
userInteraction: true, // BOOLEAN: If the notification was opened by the user from the notification area or not
message: info.message,
// userInfo: info.data,
});
const orderDetails = JSON.stringify(info);
const link = 'my-awesome-app://Private/OrderSummeryScreen/' + orderDetails;
Linking.openURL(link)
}
英文:
Using PushNotification
from the lib react-native-push-notification
. When we get new notification and app in background need to bring back app as foreground, without any interaction. We used Linking.openURL('my-awesome-app://')
, but this work when app foreground but not in background.
localNotification(info) {
PushNotification.localNotification({
channelId: 'reminders',
title: '',
userInteraction: true, // BOOLEAN: If the notification was opened by the user from the notification area or not
message: info.message,
// userInfo: info.data,
});
const orderDetails = JSON.stringify(info);
const link = `my-awesome-app://Private/OrderSummeryScreen/${orderDetails}`;
Linking.openURL(link)
}
答案1
得分: 1
在React Native中,后台操作在很大程度上取决于平台(iOS/Android)及其限制。与iOS相比,Android支持更广泛的后台操作范围。
当推送通知到达并且您希望将应用程序置于前台时,通常是由用户点击通知来完成的。在Android中,您可以使用HeadlessJS
任务在通知到达时运行一些代码,但自动将应用程序置于前台而不需要任何用户交互通常违反了Android和iOS关于应用程序行为的准则。
在iOS上,由于iOS背景限制的严格性,这是不可能的。
在Android上,从技术上讲,可以通过在本地代码(Java/Kotlin)中创建一个活动并启动它来实现,但如前所述,
这违反了Android的准则,这种行为可能导致您的应用从Google Play商店中删除。
您可以设置通知以在通知被点击时导航到应用程序中的特定屏幕,使用深度链接。您已经在Linking.openURL(link)
中进行了类似的操作,但这不会强制将应用程序置于前台。它仅在应用程序位于前台或用户与通知或链接与您的应用程序交互时起作用。
英文:
In React Native, background operation largely depends on the platform (iOS/Android) and its limitations. Android supports a broader range of background operation compared to iOS.
When a push notification arrives, and you want to bring your app to the foreground, this is typically done by the user clicking on the notification. In Android, you may use a HeadlessJS
task to run some code when a notification arrives, but automatically bringing an app to the foreground without any user interaction is generally against both Android and iOS guidelines for app behavior.
On iOS, this is simply not possible due to the strict nature of iOS's background limitations.
On Android, it could be technically possible by creating an activity in the native code (Java/Kotlin) and launching it, but as mentioned before,
this goes against Android guidelines and such behavior could result in your app being removed from the Google Play Store.
You can set up your notification to navigate to a specific screen within your app once the notification is clicked using deep linking. You're already doing something similar with Linking.openURL(link)
, but this doesn't force your app to the foreground. It only works when the app is in the foreground or if the user interacts with a notification or a link to your app.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论