在React Native中,当通知到达并且应用程序处于后台而没有用户交互时,如何执行操作



我有一个React Native应用程序,它从我的服务器接收推送通知。当应用程序处于后台并且用户打开通知时,应用程序会打印一个标签。当应用程序处于前台并收到通知时,应用程序会立即打印标签。

当应用程序处于后台时,我想在没有用户交互的情况下打印标签。

我怎样才能做到这一点?

我想使用Expo后台获取,但我需要在收到通知后立即打印标签,而使用Expo背景获取,我可能打印得太晚了。

感谢

一个选项可以是使用这个类从Java打印标签,该类覆盖expo通知服务

public class FirebaseMessageService extends FirebaseMessagingService {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

//SHOW EXPO NOTIFICATION
createNotificationsHelper().notificationReceived(createNotification(remoteMessage));


//PRINT LABEL IN JAVA...

}
public void sendRegistrationToServer(String token) {
// put your logic here to update fcm registration token
}
protected NotificationsHelper createNotificationsHelper() {
return new NotificationsHelper(this, NotificationsScoper.create(this).createReconstructor());
}
protected Notification createNotification(RemoteMessage remoteMessage) {
String identifier = remoteMessage.getMessageId();
if (identifier == null) {
identifier = UUID.randomUUID().toString();
}
JSONObject payload = new JSONObject(remoteMessage.getData());
NotificationContent content = new JSONNotificationContentBuilder(this).setPayload(payload).build();
NotificationRequest request = createNotificationRequest(identifier, content, new FirebaseNotificationTrigger(remoteMessage));
return new Notification(request, new Date(remoteMessage.getSentTime()));
}
protected NotificationRequest createNotificationRequest(String identifier, NotificationContent content, FirebaseNotificationTrigger notificationTrigger) {
return new NotificationRequest(identifier, content, notificationTrigger);
}

}

并将其添加到您的清单中:

<service
android:name=".FirebaseMessageService"
android:exported="false">
<intent-filter android:priority="-1">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

最新更新